3
$\begingroup$

I need Mathematica to remember only things upto two decimal places.

I'm currently using the unwieldy Floor[x*100]/100 which works but is there a better way to make it use two decimal places (throughout the notebook is okay).

I tried N[x,{2,2}] but although it displays the requisite two decimal places, I know that the kernel still remembers. For instance, if I try it for N[1.234,{2,2}]-1.23 is not 0. It is in fact 0.004. How can I get rid of this?

$\endgroup$
  • $\begingroup$ Examine FullForm@N[1.234, {2, 2}] and you'll see it's not doing what you think it is doing. Try instead SetAccuracy[1.234, 2] - 1.23``2. One problem using this approach is that Mathematica tracks relative precision, not a fixed number number of decimal places. Do you want fixed-point arithmetic or is just about output formatting? $\endgroup$ – Michael E2 Nov 3 '15 at 11:24
2
$\begingroup$

You want Round[] for this:

Round[1.234, 1.*^-2] - 1.23
   0.

This can be used with $PrePrint if needed.

$\endgroup$
2
$\begingroup$

To make your function work "throughout the workbook" you can use:

$PrePrint = N @ Floor[#*100]/100 &;

Now

 {1.23999, Pi}

{1.23, 3.14}

Restore $PrePrint to the default:

$PrePrint =.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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