InputForm[Round[2.40, .1]]
returns 2.4000000000000004
why isn't it 2.4 ??
how can I get it to return 2.4, or more generally, how can I get numbers approximated to n decimal places?And I mean how do I really change the value; I know I can use NumberForm to change what is printed.
Mathematica uses binary floating point representation for approximate numbers (i.e. numbers with a decimal point). That means that you can never exactly represent a rational number whose denominator is not a power of 2 with an approximate number.
Thanks, but NumberForm also only affects the print form.
I think this may actually do what I want! N[Rationalize[Round[2.40000004, .01]]]]
In[12]:= InputForm[N[Rationalize[Round[2.400004, .1]]]]
Out[12]//InputForm= 2.4
In[3]:= NumberForm[2.40, {2, 1}]
Out[3]//NumberForm= \!( TagBox[ InterpretationBox["\<\"2.4\"\>", 2.4, AutoDelete->True], NumberForm[#, {2, 1}]& ])
but it still bugs me.
Then see this Table command not obeying limits related discussion, please. The value 2.4 as a rational number is 24/10. Everything else is subject to a number representation.
Give SetPrecision a try
SetPrecision
In[4]:= SetPrecision[2.40000032783267236526523651, 2] Out[4]= 2.4 In[5]:= SetPrecision[2.40000032783267236526523651, 2] + 2. Out[5]= 4.4