how to calculate digits after decimal point?

19 Ansichten (letzte 30 Tage)
manisha sharma
manisha sharma am 20 Mär. 2015
Beantwortet: Riad am 25 Mär. 2025
Hello everyone! I want to calculate the number of digits after decimal point but want to ignore the trailing zeros. example: 1.2344500 digits after decimal should be 5.
Is there any inbuilt function to count these digits after decimal or to remove trailing zeros? Thanks in advance...
  5 Kommentare
manisha sharma
manisha sharma am 21 Mär. 2015
Bearbeitet: manisha sharma am 21 Mär. 2015
I want to hide a decimal digit after the last digit of decimal point number.For this i first calculate the number of digits after decimal and then multiply the original number with this count in power of 10.
example:
num=9.155273437500000e-005;
decplc=cntdec(num); %i have made cntdec function
decplc=15;
num=num*10^decplc;
num=9.155273437500000e+010
then i add a number to this decimal shifted number at last position.
example:
num=num+.3;
num=9.155273437530000e+010;
then i again shift all digits of the original number to right of decimal point.For this i multiply num with negative cntdec in power of 10.
example:
num=num*10^-decplc;
num=9.155273437530002e-005;
Now when i try to access the last digit of the num and for this i repeat the above procedure:
  • count digits after decimal point.example:
d=cntdec(num); d=20
this time digits after are coming out to be 20 while actually there must be only 16 digits after decimal (15 was present originally+ 1 i inserted).
  • again shift all digits to left/before decimal pointexample:
f=num*10^d
f =
9.155273437530002e+015
  • Now to get last digit i convert f into string and access digit at its last index.
example:
f=num2str(f);
f(length(f))
f =
2
But i want my answer as 3 which i have inserted at last of original number. May be i'm doing something wrong.Please guide. Please help me..... Thanks
Stephen23
Stephen23 am 21 Mär. 2015
Bearbeitet: Stephen23 am 28 Apr. 2021
Most decimal representations of binary fractional numbers do not have a finite number of digits: the fractional values that you are giving most certainly do not have "zero" for all trailing digits. You need to learn about floating point numbers, this would be a good start:
This is also a topic that has been discussed many times before:
Note also that a double floating point value only has a decimal precision of around 15 to 17 digits, which means it is possible that your "altering" of a decimal digit after the fourteenth decimal digit may be simply beyond any representable change in the floating point value: i.e. is meaningless.
You might be interested in James Tursa's submission "num2exact" which shows the decimal digits required to represent a binary floating point value:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 20 Mär. 2015
Bearbeitet: James Tursa am 23 Mär. 2015
See this related thread:
Using the function in the above link, the number of significant digits after the decimal point could be calculated as follows:
max(sigdigits(x)-floor(log10(abs(x)))-1,0) % Don't count sig digits before decimal pt
EDIT:
If you want to add a digit after the last significant digit, then something like this (assumes x is positive):
x = original_number; % E.g., 123.456
a = added_digit; % E.g., 9
y = x + a*10^(floor(log10(x))-sigdigits(x)); % New number
E.g.,
>> x = 123.456
x =
1.234560000000000e+02
>> a = 9
a =
9
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234569000000000e+02
>> x = 1.23456789123
x =
1.234567891230000
>> a = 9
a =
9
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234567891239000
>> x = 12345.12345
x =
1.234512345000000e+04
>> a = 8
a =
8
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234512345800000e+04
  3 Kommentare
James Tursa
James Tursa am 27 Mär. 2015
The term "LSB" implies that you are now working in the binary realm instead of the decimal realm. Those are two different animals and require completely different strategies than are posted in this thread. I suggest you open up a new Question stating what you are doing, exactly the data type you have, and how exactly you want to manipulate the LSB. There are several inbuilt MATLAB functions that can help with this.
manisha sharma
manisha sharma am 27 Mär. 2015
Yes, i have already done this... thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

per isakson
per isakson am 23 Mär. 2015
Another approach, which is based on sprintf and regexp
nsg = significant( 1.2344500 )
nsg = significant( 1e-16 )
nsg = significant( 1e-17 )
nsg = significant( 1.2000000 )
nsg = significant( 1.0000000 )
echos
nsg =
5
nsg =
16
nsg =
33 % <<< error?
nsg =
1
nsg =
0
where
function nsg = significant( num )
ddd = max( [ 1, -floor( log10( eps( num ) ) ) ] );
frm = sprintf( '%%.%df', ddd );
str = sprintf( frm, num );
cac = regexp( str, '(?<=\.)\d*?(?=0*+\>)', 'match', 'emptymatch' );
nsg = numel( cac{:} );
end
  2 Kommentare
James Tursa
James Tursa am 24 Mär. 2015
Bearbeitet: James Tursa am 24 Mär. 2015
nsg = significant( 1e-17 )
nsg =
33 % <<< error?
Yes, unfortunately sprintf can't be relied upon to print 0 digits when the number of digits printed gets down to eps of the number. E.g., for 1e-17 the string generated is:
.000000000000000010000000000000001
But that 1 at the end really isn't significant. E.g.,
>> isequal(.000000000000000010000000000000001,1e-17)
ans =
1
Also, the behavior of sprintf on Windows vs Unix can be very different when printing out these ending digits. E.g., on Windows I get this:
>> num2strexact(1e-17)
ans =
1.00000000000000007154242405462192450852805618492324772617063644020163337700068950653076171875e-17
>> sprintf('%97.92e',1e-17)
ans =
1.00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000e-17
So on Windows those trailing digits get rounded up to 1 regardless of how many more digits you print. However, on Unix systems a different algorithm is used so that sprintf has essentially the behavior of num2strexact (with rounding depending on number of digits printed out). In fact, this difference in behavior is why I wrote num2strexact in the first place. So getting a working algorithm that depends on the behavior of sprintf for these trailing digits is going to be very difficult.
For reference, the next lower number in IEEE double is:
>> num2strexact(1e-17-eps(1e-17))
ans =
9.999999999999999174680285036430562640498207781290622431225045829705777578055858612060546875e-18
Murat Balc?
Murat Balc? am 27 Apr. 2021
function decimal=digit(x)
x=abs(x);
y=x*10^0;
a=round(x(:)-floor(x(:)),8);
b=num2str(a);
[d c]=size(b);
decimal=max(c)-2;

Melden Sie sich an, um zu kommentieren.


Riad
Riad am 25 Mär. 2025
You should use extractAfter function, strrep can also remove the tailling zeros
>> num = '1.2344500';
decimals = extractAfter(num,'.')
decimals =
'2344500'

Kategorien

Mehr zu Elementary Math finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by