Message-ID: <1343981517415-4639018.post@n4.nabble.com>
Date: 2012-08-03T08:11:57Z
From: Berend Hasselman
Subject: Formatting numbers for display
In-Reply-To: <C7484B6E-67F0-45A9-B8B8-4E6B7DDE5561@comcast.net>
David Winsemius wrote
>
> I didn't get the same results as requested with a wider variety of
> tests using those formatting methods. Here's what I could offer:
>
> form3 <- function (x) switch(findInterval(x, c( 0, 1, 10^7, Inf)),
> format(x, digits=3),
> formatC(x, width=8, format="f",
> drop0trailing=FALSE),
> trunc(x) )
>
> tx <- c( 0.55555555, 55.555555555, 555555555555555.55555555)
>
> > sapply(tx, form3)
> [1] "0.556" " 55.5556" "555555555555555"
>
> (Still not getting the 8 digits in the intermediate value cases. Have
> not figured out how to get rid of leading space.)
>
How about this to get rid of leading space:
form3 <- function (x) sub("^ +","", switch(findInterval(x, c( 0, 1, 10^7,
Inf)),
format(x, digits=3),
formatC(x, width=8, format="f",
drop0trailing=FALSE),
trunc(x) )
)
Berend
--
View this message in context: http://r.789695.n4.nabble.com/Formatting-numbers-for-display-tp4638991p4639018.html
Sent from the R help mailing list archive at Nabble.com.