# How to keep the two digits after the decimal point

If I use NumberForm,it is a very nice answer

SeedRandom[1]
result1 = NumberForm[#, {Infinity, 2}] & /@ RandomReal[20, 20]


{16.35,2.23,15.79,3.76,4.83,1.31,10.84,4.62,7.92,14.01,4.24,14.97,8.46,4.95,19.54,16.50,18.51,11.56,5.86,4.16}

Just one defect,it is not can do any calculation with it anymore.Such as we cannot expect result1+2 will run normally.So I use Export and Import to process it like

result=Import[Export["tem.txt", result1, "List"], "List"]


{16.35,2.23,15.79,3.76,4.83,1.31,10.84,4.62,7.92,14.01,4.24,14.97,8.46,4.95,19.54,16.50,18.51,11.56,5.86,4.16}

The result is exactly what I want.But is there any elegant method can do this?

• The two methods are contradictory in that they do completely different things. So it remains unclear what you actually want. Apparently, you're expecting to have all numerical output rounded (as in the second example). That would lead to all sorts of trouble in reusing the results, so it appears to be a very bad idea.
– Jens
Commented Apr 26, 2017 at 3:44
• Maybe you want this: NumberForm[result1=RandomReal[20,20],{Infinity,2}]
– Jens
Commented Apr 26, 2017 at 3:50

\$PrePrint = NumberForm[#, {Infinity, 2}] &;

• Good lesson for usage of Infinity.But actually we have not get those numbers what I want,just show it?I mean,you have not get a result1 with two digits after the decimal point,just show it in that form.
– yode
Commented Apr 26, 2017 at 2:55
• You mean you're trying to mimic the behavior of… Er… rounding calculation by hand? Commented Apr 26, 2017 at 3:16
• Yep,something like that..
– yode
Commented Apr 26, 2017 at 3:29
• And I have include your usage of Infinity to my post.Hope do not mind that.
– yode
Commented Apr 26, 2017 at 3:31
• Well, then I should say I doubt if simply controling the digit of number will work. As far as I can tell, many posts tagged with [precision] and [accuracy] have suggested that, the… Er … rounding rule of Mathematica and manual calculation have subtle difference, though their behaviors are similar. BTW, do you know there's no one-to-one correspondence between decimals and floating number? (See e.g. docs.python.org/3/tutorial/floatingpoint.html ) Commented Apr 26, 2017 at 3:47