# How to apply a function to coefficients of a polynomial?

I found Apply a function to all coefficients of a polynomial but I could not understand

I want to apply Round to the coeffricients of $$x^2 + 3.1x^3 + 5.4x^4$$. The Map function tries to Round the entire expression like 3.1x^3

• Try: x^2 + 3.1 x^3 + 5.4 x^4 /. x_?NumericQ :> Round[x] This results in: x^2 + 3 x^3 + 5 x^4 Mar 2, 2023 at 17:48

I would use 3-arg Collect for this purpose:

Collect[x^2+3.1 x^3+5.4 x^4, x, Round]


x^2 + 3 x^3 + 5 x^4

• Note that if there is a variable in any of the coefficients, this does not behave as expected, e.g., Collect[a*x^2 + 3.1 x^3 + 5.4 x^4, x, Round] Mar 2, 2023 at 20:21
• Why I cannot turn this into a function? polyRound[poly0_] := Collect[poly0_, x, Round] gives Round[Pattern[ 2 x +1.5 3 x ,_]]  Mar 3, 2023 at 11:15
Clear[x, expr]
expr = x^2 + 3.1 x^3 + 5.4 x^4;
crules = Merge[CoefficientRules[expr], Round @@ # &] // Normal

FromCoefficientRules[crules, Variables[expr]]


x^2 + 3 x^3 + 5 x^4