I came across a situation where many results got many decimal places. For further calculations in my situation, I would just take the value as it shows on the screen and drop further digits. For example, I have aa = Pi/2 // N
, which shows on my screen as 1.5708
while its actual value is 1.5707963267948966'
. I would like to have a process that turns this value aa
with many digits to its approximation bb
which would be just 1.5708
. I do not want to specify number of decimal places, but will take what appears on the screen (since other results may have a different number of decimal places). Also, this bb
needs to be able for use towards further calculations, such that bb + 1
will give me 2.5708 exactly.
3 Answers
aa = Pi/2 // N;
bb = ToExpression[ToString[aa]]
1.5708
bb + 1
2.5708
-
$\begingroup$ I've long used the following which is similar to Chris's answer. $\endgroup$ Nov 9 at 6:33
I would like to have a process that turns this value aa with many digits to its approximation bb which would be just 1.5708
If you mean here a "manual process", then you can do a "copy as plainText" -> "Paste"
Compare with what happens with a normal Copy-> Paste (on Windows: Crtl-C -> Ctlr-V)
-
$\begingroup$ Sorry for the ambiguity, but I was looking for a function type of process. Thanks though. $\endgroup$– nanjunFeb 3, 2020 at 15:55
I've long used the following which is similar to Chris Dengen's answer above. However the addition of NumberForm, InputForm and a parameter n make this a workhorse for me.
Why would I need this in Mathematica? One example. Suppose I do a model fit to data of lower precision and get back model parameters to 15 decimal points. No way. Using parameters in my model with that much precision conveys a lack of understanding on my part of the approximate nature of the model. And perhaps confuses the model user. So I convert the list of parameters using sigFig
before embedding it in the model function that I deliver to a customer. (I have never understood why this isn't built-in to Mathematica.)
sigFig::usage = "sigFig[4.912238383] returns 4.91, sigFig[4.912238383,1] returns 5";
sigFig[x_, n_:3] :=
ToExpression[ToString[NumberForm[x, n], InputForm]];
test = sigFig[4.91223, #] & /@ Range[1, 20];
Options[$FrontEnd, PrintPrecision]
, so you could just useRound[]
along with that setting. $\endgroup$