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
Internal`StringToDouble@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=Internal`StringToDouble@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