[R] How do I format something as "0.000"?

Dirk Eddelbuettel edd at debian.org
Mon Jan 17 21:19:29 CET 2005


On Mon, Jan 17, 2005 at 04:49:35PM +0100, Christian Hennig wrote:
> Hi,
> 
> I would like to use the format function to get numbers all with three 
> digits to the right of the decimal point, even in cases where there is no
> significant digit left. For example, I would like to get
> c(0.3456789,0.0000053) as "0.346" "0.000".
> It seems that it is not possible to force format to print a "0.000", i.e.
> without any significant decimal places.
> Is it possible to do this somehow in R?

sprintf can do that

	> sprintf("%3.3f %3.3f", 0.3456789, 0.00000053)
	[1] "0.346 0.000"

You'd have to loop over your vector to do it one by one, I think.

Hth, Dirk

-- 
Better to have an approximate answer to the right question than a precise 
answer to the wrong question.  --  John Tukey as quoted by John Chambers




More information about the R-help mailing list