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

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] := 
     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.

  • 2
    $\begingroup$ It seems this suits Your needs: expr /. x_Real :> Round[x, .01] $\endgroup$
    – Kuba
    Jun 13, 2013 at 9:25
  • 2
    $\begingroup$ ...alternatively, expr /. x_?InexactNumberQ :> Round[x, 0.01]. $\endgroup$ Jun 13, 2013 at 9:26
  • $\begingroup$ @Kuba Thank you, it indeed suits my need. $\endgroup$ Jun 13, 2013 at 11:50
  • $\begingroup$ 0x4A4D Thank you. $\endgroup$ Jun 13, 2013 at 11:51

1 Answer 1


As regards your final comment: "I cannot understand why these decimal points show up", and interested by your use of Element, it appears the integers are getting converted because they are found to be in the domain of real numbers, which I wouldn't have expected.

Element[1, Reals]


So your function can be made to work thusly:

rnd[expr_, m_Integer] := Map[If[NumberQ[#],
    If[Element[#, Reals] && Not[Element[#, Integers]],
     (Round[#*10^m]/10^m // N), #], #] &, expr, {1, Depth[expr]}]
  • 2
    $\begingroup$ Succinctly put, 1 is real, but not Real, while 1. is both real and Real. The "Mathematica sense" of Real (inexact number with zero imaginary part) versus the "mathematics sense" of real (any admissible element of $\mathbb R$), which Element[] tests for, are seen to be different. $\endgroup$ Jun 13, 2013 at 16:10
  • $\begingroup$ @Chris Degnen Thank you. $\endgroup$ Jun 14, 2013 at 8:48
  • $\begingroup$ @Chris, actually it is a useful comment $\endgroup$ Jun 18, 2013 at 11:11

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.