Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

marked as duplicate by Artes, Yves Klett, Sjoerd C. de Vries, halirutan, J. M. Apr 24 '13 at 10:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@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
up vote 1 down vote accepted


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

do what you want?

share|improve this answer
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
@niren.That's why I differentiated between the value of the result and its displayed form. The number Mathematica displays is not actually exactly what it has in memory, but only to, I believe, 6 decimal places. So, Floor alone is sufficient to restrict the number of places to two. If you want it to display that way you have to change formatting rules, for instance, with NunberForm ( but there are more possibilities). – Sjoerd C. de Vries Apr 24 '13 at 9:39
@SjoerdC.deVries I edited my question.can you check once – subbu Apr 24 '13 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.