I am trying to make a function that would make rounding real numbers in an expression possibly consisting of real numbers, integer numbers and symbols. An example of such an expression is below:
expr = 1.2345 + 3.2471*x + 4.3946*x^2 + 5.56789*x^3 + 3.4812*x^4 +
1.34682*x*Exp[-2.3467*x^3] + 13.25684*x^3*Exp[23.9476*x^7] +
Sin[34.876459*x^2];
What I want to achieve is the following. Let us say, I would like to have all real numbers to have not more that two figures after comma. The expression then should then look as follows:
1.23 + 3.25 x + 1.35 E^(-2.35 x^3) x + 4.39 x^2 + 5.57 x^3 +
13.26 E^(23.95 x^7) x^3 + 3.48 x^4 + Sin[34.88 x^2]
Such a function is useful, to show dynamically and in a compact form a result obtained in the course of dynamic calculation. For example, I often fit data to a function, the both being dependent upon parameter(s). To show the fitting function dynamically in the same Panel or Manipulate window I need to cut number of figures after comma of real numbers.
At present I am here:
rnd[expr_, m_Integer] :=
Map[If[NumberQ[#],
If[Element[#, Reals], (Round[#*10^m]/10^m // N),
IntegerPart[#]], #] &, expr, {1, Depth[expr]}];
Here expr is any expression to subject the rounding, m is the integer equal to the number of figures after comma to be left.
This function almost works. Its application to the expression given above yields the following:
rnd[expr, 2]
(*
1.23 + 3.25 x + 1.35 E^(-2.35 x^3.) x + 4.39 x^2. + 5.57 x^3. +
13.26 E^(23.95 x^7.) x^3. + 3.48 x^4. + Sin[34.88 x^2.]
*)
You may see that the difference between what I get and what I want is that the powers are 2., 3. and so on instead of 2, 3 and so on. I cannot understand, why these decimal points show up, and how to get rid of them.
expr /. x_Real :> Round[x, .01]
$\endgroup$expr /. x_?InexactNumberQ :> Round[x, 0.01]
. $\endgroup$