Rounding a number

I needed to do one function. It worked with fractional numbers. Everything worked well, but only when I was working with numbers up to 0.01. But with the number 0.564, things started to work differently. I figured it out for a long time, and suddenly decided to connect the tweak number with the watch. and he began to round the numbers for me ((how can I avoid this so that he does not round?

The “correct” answer depends on what you are actually trying to do.

Do you just want 3-digits of accuracy? Use format-number and specify 3 digits.

You want to always round down for 2 digit accuracy? multiply by 100 -> floor -> divide by 100.


I want to get the number of decimal places: 1.34 = 2; 1.4567 = 4; 1.2 = 1.
this is my way:

That’s a not-trivial problem. You are looking at a fractional decimal number, but computers don’t think in decimal or fractions. The closest they come is floating point binary numbers, and you can get rounding errors converting between the two; especially if you start doing division on the numbers.

I’m assuming you won’t have trailing zeros. The “safest” way to count digits might be to convert to a string with some maximum of number of decimals, then subtract the number of trailing zeros.

Generally, it is not useful to know number of decimals anyway since it is very arbitrary (and perhaps infinite). More decimal places implies greater precision, but trying to print all digits after doing division is only giving a false impression of greater precision.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.