Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

1  
@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
1  
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
show 8 more comments

1 Answer 1

up vote 1 down vote accepted

Does

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
1  
@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
show 4 more comments

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