Skip to main content
added 230 characters in body
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

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]

True

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

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]

True

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]

True

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]}]
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

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]

True