I have an expession (it is the result of Fit) but I want to print or display an abbreviated form with all numbers in it rounded to say 6 decimal digits.

Print[N[polynomial, 6]] does not what I want and ToString also has no option to limit the number of digits.

I rather get an expression with 0.0008834432170369743*d in it (the other coefficients also have physically meaningless digits which I would like to get rid of).

How can I transform an expression to a string limiting all numerical constants in it to some given number of relevant digits?

I further want to use such a string for fit in a constuct like this:

    Show[{ Graphics[myPlot]
         , Graphics[{Black
                     , Text[fit
                           , {2, 1}
                           , {-1, 0}
  • $\begingroup$ Perhaps expr = 0.0008834432170369743*d; Map[ If[NumericQ[#], N[Round[#, 10^-6]], #] &, expr, Infinity] $\endgroup$
    – Bill
    Commented Feb 6, 2017 at 18:04
  • $\begingroup$ Why does it have to be a string? $\endgroup$
    – Carl Woll
    Commented Feb 6, 2017 at 19:38
  • $\begingroup$ Carl: I want to show it on a plot. I came across Text for that purpose. But you are right, Text[fit[d], {1, 2*(h/3)}, {-1, 0}] would also work, but I can't combine that with an explanation "fit=...". Also the polynomial is no longer sorted by ascending power: in my example the squared term comes first (the coefficient for it is much smaller than the one for the linear term). $\endgroup$ Commented Feb 6, 2017 at 20:34

2 Answers 2

expr = 0.0008834432170369743*d;

expr /. x_?NumericQ :> Round[x, 10.^-6]

(*  0.000883 d  *)

However, if the the expression contains exact constants that you wish to retain, compare

expr = 0.0008834432170369743*E^(-1.234567890123456789 x) Sin[Pi x/2];

expr /. x_?NumericQ :> Round[x, 10.^-6]

(*  0.000883 2.71828^(-1.23457 x) Sin[1.5708 x]  *)

expr /. x_Real :> Round[x, 10.^-6]

(*  0.000883 E^(-1.23457 x) Sin[(π x)/2]  *)
  • $\begingroup$ Bob, strangely on my Mma Release (5292910, 2015032401) Round[0.0008834432170369743, 10.^-6] yields 0. Is this a bug? expr /. x_?NumericQ :> Round[x, 10.^-6] also yields 0. The same holds for your other example. $\endgroup$ Commented Feb 6, 2017 at 20:58
  • $\begingroup$ @AdalbertHanßen - I get the expected result with versions 9.0.1, 10.4.1, and 11.0.1 on a Mac. I don't have v10.1.0 to test on. What OS are you on? Maybe someone else can duplicate the problem you are having. $\endgroup$
    – Bob Hanlon
    Commented Feb 6, 2017 at 21:11
  • $\begingroup$ Bob, it happened on my Mma at home running on Linux (Xubuntu 14.04). $\endgroup$ Commented Feb 8, 2017 at 17:25
  • $\begingroup$ @AdalbertHanßen - Only someone with that version and OS can verify the problem with that setup. $\endgroup$
    – Bob Hanlon
    Commented Feb 8, 2017 at 17:37
  • $\begingroup$ I found my error: I used some package from early Mma4.0 times when there was a Round function with only one parameter. I had overloaded this function with a two parameter version where the second parameter was the exponent at base 10 to round to. That's something completely different to today's 2 parameter version of Round! Sorry for the confusion caused to all others. Now I get your results. $\endgroup$ Commented Feb 9, 2017 at 16:22

Perhaps something like the following:

Text[Row[{"fit = ", RandomReal[1, 3].{1, d, d^2}}], 
FormatType -> StandardForm, BaseStyle -> {PrintPrecision -> 2}]

Row addresses your need to augment the output with "fit = ". Using StandardForm (instead of the default TraditionalForm) changes polynomial ordering. The option PrintPrecision controls how much precision is displayed

  • $\begingroup$ Thank you for your hint. In particular thank you for the explanation because the explanation of Text misses a reference to Row. $\endgroup$ Commented Feb 10, 2017 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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