Keeping trailing zeros

2019-01-08 12:16发布

问题:

In R, 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.

Can you help me?

回答1:

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.



回答2:

When you print it out, you should be able to do:

formatC( round( 5.2, 3 ), format='f', digits=3 )


标签: r math rounding