# How can I Influence the number of digits when printing a polynomial expression or when I want to make it a String?

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}
]
}
]
}
]

• Perhaps expr = 0.0008834432170369743*d; Map[ If[NumericQ[#], N[Round[#, 10^-6]], #] &, expr, Infinity] – Bill Feb 6 '17 at 18:04
• Why does it have to be a string? – Carl Woll Feb 6 '17 at 19:38
• 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). – Adalbert Hanßen Feb 6 '17 at 20:34

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]  *)

• Bob, strangely on my Mma Release 10.1.0.0 (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. – Adalbert Hanßen Feb 6 '17 at 20:58
• @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. – Bob Hanlon Feb 6 '17 at 21:11
• Bob, it happened on my Mma at home running on Linux (Xubuntu 14.04). – Adalbert Hanßen Feb 8 '17 at 17:25
• @AdalbertHanßen - Only someone with that version and OS can verify the problem with that setup. – Bob Hanlon Feb 8 '17 at 17:37
• 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. – Adalbert Hanßen Feb 9 '17 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

• Thank you for your hint. In particular thank you for the explanation because the explanation of Text misses a reference to Row. – Adalbert Hanßen Feb 10 '17 at 9:09