Keeping trailing zeros

Marco picture Marco · Mar 28, 2011 · Viewed 37.6k times · Source

I would like to keep trailing zeros, for example, if I type:

round(5.2, 3)

I would like the output to be:

5.200

Answer

Chase picture Chase · Mar 28, 2011

If this is for printing purposes, sprintf is what you are after:

> sprintf("%.3f", round(5.2,3))
[1] "5.200"

See ?sprintf for formatting details.