-3
$\begingroup$

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.

$\endgroup$
13
  • 1
    $\begingroup$ @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. $\endgroup$
    – subbu
    Apr 24, 2013 at 7:41
  • 1
    $\begingroup$ 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? $\endgroup$
    – Yves Klett
    Apr 24, 2013 at 7:44
  • $\begingroup$ @YvesKlett it's precision problem.I will edit my question. $\endgroup$
    – subbu
    Apr 24, 2013 at 7:47
  • $\begingroup$ @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. $\endgroup$
    – Artes
    Apr 24, 2013 at 8:53
  • $\begingroup$ @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. $\endgroup$ Apr 24, 2013 at 9:29

1 Answer 1

1
$\begingroup$

Does

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

do what you want?

$\endgroup$
9
  • $\begingroup$ sorry,your code not satisfying the example 1and example 4. $\endgroup$
    – subbu
    Apr 24, 2013 at 9:08
  • $\begingroup$ @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 $\endgroup$ Apr 24, 2013 at 9:26
  • $\begingroup$ @SjoerdC.deVries If I use NumberForm with Floor, I end up with not number. $\endgroup$
    – niren
    Apr 24, 2013 at 9:31
  • 1
    $\begingroup$ @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). $\endgroup$ Apr 24, 2013 at 9:39
  • $\begingroup$ @SjoerdC.deVries I edited my question.can you check once $\endgroup$
    – subbu
    Apr 24, 2013 at 9:42

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