3
$\begingroup$

How can I change an expression like

A = (-1. a)/(x + y) - (2.1 b)/(x + y) + 4.1 + 2. x + (3. r s)/(x + y)

so that -1. a becomes a, 2. x becomes 2 x and 3. r s becomes 3 r s yet 2.1 b and 4.1 remain unchanged? Rationalize[A] almost works except it changes 2.1 b and 4.1. The expression could be even more complex.

$\endgroup$

1 Answer 1

3
$\begingroup$
A /. x_Real /; FractionalPart[x] == 0 :> Rationalize[x]
4.1 + 2 x - a/(x + y) - (2.1 b)/(x + y) + (3 r s)/(x + y)
$\endgroup$
3
  • $\begingroup$ Perfect!. Thank you. Just what I need. $\endgroup$ Commented Jan 14, 2021 at 19:27
  • 2
    $\begingroup$ Also, A /. x_Real /; IntegerPart[x] == x :> IntegerPart[x] $\endgroup$ Commented Jan 14, 2021 at 21:02
  • $\begingroup$ Again, perfect. Thank you, Bob. $\endgroup$ Commented Jan 15, 2021 at 18:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.