Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping trailing zeros

Tags:

math

rounding

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 
like image 937
Marco Avatar asked Mar 28 '11 12:03

Marco


People also ask

How do you keep trailing zeros?

Use the pound sign to prevent extra zeroes. The symbol # is another placeholder character in custom formats. This will prevent all leading zeroes if used at the front of the number, and prevent all trailing zeroes if used after the decimal point. For example, the custom format 00.

Should you use trailing zeros?

For example, in pharmacy, trailing zeros are omitted from dose values to prevent misreading. However, trailing zeros may be useful for indicating the number of significant figures, for example in a measurement. In such a context, "simplifying" a number by removing trailing zeros would be incorrect.

What is the rule for leading and trailing zeros?

1) Leading zeroes shall be suppressed, although a single zero before a decimal point is allowed. 2) Significant zeroes shall not be suppressed; when a single zero is significant, it may be used. 3) Do not include Trailing zeroes after the decimal point unless they are needed to indicate precision.

How do I keep 0 after decimal in Excel?

Get to the Format Cells dialog (e.g. by right-clicking a cell and selecting "Format Cells...") On the Number tab, select Number from the list of Categories. Set the Decimal Places box to 6. Hit Ok .


2 Answers

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.

like image 125
Chase Avatar answered Oct 17 '22 05:10

Chase


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

formatC( round( 5.2, 3 ), format='f', digits=3 ) 
like image 30
tim_yates Avatar answered Oct 17 '22 03:10

tim_yates