how to calculate the number of digits after decimal point?

7 Ansichten (letzte 30 Tage)
say i have a data a=1.6556, somehow i need to use a function which gives 6556 i.e., digits after the decimal, and if a=256 then the result should be zero. please help

Akzeptierte Antwort

Greig
Greig am 30 Aug. 2015
Try something like this
a = 1.6556;
splt = regexp(num2str(a), '\.', 'split');
dps = str2num(splt{2})
  1 Kommentar
Walter Roberson
Walter Roberson am 30 Aug. 2015
This is not actually correct, though it would certainly usually look that way. See my Answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 30 Aug. 2015
Bearbeitet: Walter Roberson am 30 Aug. 2015
If you are on MS Windows then you need to get num2strexact() from the File Exchange. Then you can
a = 1.6556;
splt = regexprep(num2strexact(a), '^[^.]*\.', '');
dps = str2double(splt);
You will find that the answer is approximately 6.556e+51 and that the exact answer, available as a string in splt, is 6555999999999999605648781653144396841526031494140625
Why is that the exact answer and not 6556 ? It is because 0.6556 cannot be represented exactly in binary floating point arithmetic, because 1/10 is an infinite repeating number in binary just as 1/7 is an infinite repeating number in decimal. The number that is actually stored for 1.6556 is 1.6555999999999999605648781653144396841526031494140625 . You do not see this because the "format" you are using is rounding off to 4 decimal places.
If you had a = 1.3733243 then the typical "format short" that is in effect would display
a =
1.3733
What value would you want as output? The 3733 that it was displayed as, or the 3733243 that it was assigned as? Probably what it was assigned as. But once it is assigned then all MATLAB knows is the internal numeric representation, in binary floating point, and the actual internal representation of 1.3733243 is 1.3733242999999999423010876853368245065212249755859375 . Those digits are "really there", just the same way that the .3733243 is "really there" even though only 1.3733 was displayed.
If you only want the answer to a certain number of decimal places, e.g., D = 5 for 5 decimal places, then use
x = abs(a); %account for negative numbers
round((x - floor(x)) * 10^D)
  6 Kommentare
Walter Roberson
Walter Roberson am 30 Aug. 2015
I am not sure which is my "second code".
I caution again that the sprintf() version is only suitable for OS-X as MS Windows does not support extended field widths at all and Linux gets the answers wrong after some number of decimal points.
Another thing that I would point out is that if you are going after the integer representation rather than the string representation, then you are not able to distinguish between 0.123 and 0.0000123 as 123 and 0000123 have the same integer value.
Paromita Bhattacharjee
Paromita Bhattacharjee am 30 Aug. 2015
I meant the code before the OS-X code. Thank you for all your help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by