Keeping trailing zeros

RMathRounding

R Problem Overview


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

R Solutions


Solution 1 - R

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.

Solution 2 - R

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcoView Question on Stackoverflow
Solution 1 - RChaseView Answer on Stackoverflow
Solution 2 - Rtim_yatesView Answer on Stackoverflow