Count number of digits after decimal point?

59 views (last 30 days)
Hi,
I want to calculate the number of digits after the decimal point with the trailing zeros. I have tried a lot of methods but no luck.
For example: If I input '0.2500', the output should be '4', means the trailing zeros should be counted as well.

Accepted Answer

John D'Errico
John D'Errico on 14 Mar 2022
MATLAB does not distinguish between numbers with trailing zeros after the decimal point. That is, 0.25 == 0.250 == 0.250000000. They are all identically the same numbers. (I won't even get into the question about if 0.3 is really exactly the fraction 3/10, as many people expect. It is not.)
However, if your number is in character representation, as you have written it, then counting the number of digits after the decimal point is easy. Just locate where the decimal point lies. Then locate the last digit in the number. Assuming the string in question is only one number, and there are no trailing spaces, then this will work:
N = '0.25000';
numel(N) - find(N == '.')
ans = 5
You would need to worry about cases with trailing white space, then worry about cases where there was no decimal point, like the number '25', which has zero digits to the right of the decimal point. The trick there, is if a number has no decimal point, then implicitly, there is a decimal point at the very end. So if there is no decimal point found, then add a decimal point as a new last character to the number.
  3 Comments
Steven Lord
Steven Lord on 14 Mar 2022
x2 = 0.20
x2 = 0.2000
x4 = 0.2000
x4 = 0.2000
x2 == x4 % true
ans = logical
1
There's no way to distinguish, once a variable was created, whether the first or second line (x2 or x4) was used to create them. The two numbers have exactly the same bit pattern.
format hex
x2
x2 =
3fc999999999999a
x4
x4 =
3fc999999999999a
John D'Errico
John D'Errico on 14 Mar 2022
I said this in my answer, and as Steve said. Once you create a NUMBER, then the number of trailing zeros is lost.
0.1 == 0.1000000000
ans = logical
1
The two numbers are now identical. It is now impossible to distinguish how many trailing zeros were in there.
And that is why I said you need to keep the numbers in purely character form if you really need to count the number of digits after a decimal point. Even at that, be careful, since counting the number of digits after the decimal point for a number like 0.7 is subtly difficult.
x = 0.7
x = 0.7000
sprintf('%0.55f',x)
ans = '0.6999999999999999555910790149937383830547332763671875000'
So as you see, the number actually stored in a double precision number is not exactly 0.7, but 0.699999999...
And that begs the question, of exaclt how many trailing digits there are in any such number. For example, how many trailing digits are there in this number:
format long g
y = 1/7
y =
0.142857142857143

Sign in to comment.

More Answers (1)

Jan
Jan on 14 Mar 2022
If your input is '0.2500', it is not a number, but a char vector. Then considering the trailing zeros is possible. But if you are talking about numbers, you have to consider the general limitations mentioned by John.
Remember, that numbers are stored in binary format and only displayed in decimal format in the command window. All internal computations are performed in the IEEEE-754 format. Most decimal values do not have an exact binary representation and the same is true the other way around. This causes effects like:
0.1 + 0.2 - 0.3 == 0 % FALSE!
If the output is displayed with 0 decimal digits, 0.0000 is shown, but the real answer differs from 0. How many digits 0.549999999999999 has is a question of taste anbd you cannot get a unique answer.
So please clarify, if you are talking about numbers like 0.2500 or char vectors like '0.2500'. For the latter:
s = '0.2500';
k = find(s ~= '0', 1, 'last')
  2 Comments
Mujtaba Farrukh
Mujtaba Farrukh on 14 Mar 2022
Thanks a lot for your detailed explaination. I want to do it for numbers. The quotes that I put in the question is to just show the numbers clearly. Is there any way like to store them in a cell or convert them in char or string to get the required answer?
Example: if input is a=5.2000, the output should be 4 or a=5.000 then the output should be 3.
Jan
Jan on 14 Mar 2022
Again: For Matlab and other numerical software, there need not be a difference between 5.2000 and 5.1999999999999999 . If you just crop the trailing digits or decide for a certain number of digits for rounding up or down, the result will differ.
You are asking for a more or less arbitrary result.
Think twice about what this means:
0.1 + 0.2 - 0.3 == 0 % FALSE!
You do need another approach. If the number of trailing zeros matter, working with doubles cannot solve the problem. You do have to work with char vectors to represent the decimal digits. Maybe a UINT8 vector for storing the digits is working also.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!