# How can I solve precision problem [duplicate]

I want to set 2 decimal places, whether it's real Number or anything.for that purpose I wrote the following function.

 decimalPlaces[number_]:=SetPrecision[number, 3]


case 1:

If you pass 3as a argument,it should returns 3.00.

case 2:

if you pass 3.98967465684 as a argument,it should returns 3.98.

case 3:

if you pass 394 as a argument,it should returns 394.00.But it returns 394.

case 4:

if you pass 394.985674 as a argument,it should returns 394.98.But it returns 394.

finally,my function was not satisfying the case 3,4 testcases

How can I solve this.

Fell free,If you want to edit my question.

Thank you.

• @Artes you used NumberForm but, NumberForm acts as a "wrapper", which affects printing, but not evaluation. But in my program, after completion of the above calculation,I am doing some computation by using the above value. – subbu Apr 24 '13 at 7:41
• Your question title is misleading. It is neither a precision problem nor a problem with Mathematica. Do you want a special kind of rounding or display? – Yves Klett Apr 24 '13 at 7:44
• @YvesKlett it's precision problem.I will edit my question. – subbu Apr 24 '13 at 7:47
• @subbu The linked post comletely answers your question. If you are confused you should think about e.g. this Table[N[3.999999976565463548438, k], {k, 10}]. It says enough to understand your problem. – Artes Apr 24 '13 at 8:53
• @artes if you look at example 2 you see the question is not purely about the number of digits. There's also some kind of rounding down involved. – Sjoerd C. de Vries Apr 24 '13 at 9:29

decimalPlaces[number_] := SetAccuracy[Floor[number, 0.01], 3]

• sorry,your code not satisfying the example 1and example 4. – subbu Apr 24 '13 at 9:08
• @subbu you weren't very clear about whether you wanted the output to look like that or whether its value should be like that. In the former case just use NumberForm in addition to Floor – Sjoerd C. de Vries Apr 24 '13 at 9:26
• @SjoerdC.deVries If I use NumberForm with Floor, I end up with not number. – niren Apr 24 '13 at 9:31