How to prevent Round with hidden fractions

I found a strange behavior in Round. If we try:

ToString[Round[4.811, 0.01], InputForm]


we get:

4.8100000000000005

When I expected

4.81

In order to get what I need, we can do something like (@Pickett and my suggestions):

N@FromDigits@RealDigits@Round[4.811, 0.01]


or

InternalStringToDouble@ToString@Round[4.811, 0.01]


There is a simpler solution for this?

Speed teste

range=Range[1,1000,0.01];
(t1=N@FromDigits@RealDigits@Round[#,0.01]&/@range)//AbsoluteTiming//First
(t2=InternalStringToDouble@ToString@Round[#,0.01]&/@range)//AbsoluteTiming//First


0.999

0.460

Update

Important note. @BobHanlon solution stop to work for lists with more than 99 elements. Mathematica compiles de expression N@Round[#,1/100]&/@list, and the unwanted behavior came back. To prevent that, I used Listable Round attribute.

Similar question: Rounding problems inside InputField

• Related: (7871), (55292), Nov 10, 2014 at 13:13
• This saved my day! Thank you, Murta Mar 17, 2019 at 7:27

myround is undefined. I assume that you mean Round.

ToString[Round[4.811, 0.01]]


"4.81"

ToString[Round[4.811, .01 // Rationalize] // N, InputForm]


"4.81"

• Yes, corrected myround to round. N@Round[4.811, 1/100] is much faster. tks +1 Nov 10, 2014 at 9:51

I suggest that you should be using a number formatting function, e.g. NumberForm, for the kind of control you are apparently after.

For Round and ToString only you could use OutputForm:

ToString[Round[4.811, 0.01], OutputForm]

"4.81"


Which is the default and therefore equivalent to this on an unmodified installation:

ToString @ Round[4.811, 0.01]


a = Round[4.811, 0.01];
b = N @ FromDigits @ RealDigits @ Round[4.811, 0.01];

ToString[{a, b}, InputForm]

RealDigits[{a, b}]

"{4.8100000000000005, 4.81}"

{{{4, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1},
{{4, 8, 0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, 1}}


Although b happens to print as you desire you can see that its decimal conversion (with RealDigits) is not as "good" as the simple Round result. Neither number is accurate because you cannot represent 481/100 as a machine precision binary number:

RealDigits[481/100, 2]

{{1, 0, 0, 1, 1, {0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0}}, 3}


Therefore this is unlikely to be a desirable numeric operation, returning us again to the domain of output formatting. What exactly is your intention?

• Tks @Mr.Wizard, but I need it as numbers. Now I'm using N@Round[4.811, 1/100]. I have some problems with automatic compilation, but now it's ok. OutputForm has problems with big numbers, and InputForm works just nice. Nov 10, 2014 at 13:18