Hi All, I have am using Sweave and the \Sexpr{} to place some numeric variables in my tex document. I want to format the number prior to entry so they read slightly more elegantly. Say i have the following numbers x <- 0.00487324 y <- 0.000000432 z <- 0.567 I would like to have the numbers displayed as follows x1 <- 0.0049 y1 <- 0.00000043 z1 <- 0.57 I've seen i can use sprintf("%.3f", pi) for example to get the formating after the decimal place, but i can't figure out an elegant way to find the position of the first non-zero entry to allow me to substitute this value into the sprintf command. Can anyone offer any advise? Thanks in advance Mike [[alternative HTML version deleted]]
Michael Pearmain-2 wrote:> > Hi All, > > I have am using Sweave and the \Sexpr{} to place some numeric variables in > my tex document. I want to format the number prior to entry so they read > slightly more elegantly. > Say i have the following numbers > x <- 0.00487324 > y <- 0.000000432 > z <- 0.567 > > I would like to have the numbers displayed as follows > > x1 <- 0.0049 > y1 <- 0.00000043 > z1 <- 0.57 > >\Sexpr{round(x,2)} Dieter -- View this message in context: http://old.nabble.com/Numeric-formatting-question-tp26283386p26283422.html Sent from the R help mailing list archive at Nabble.com.
On Nov 10, 2009, at 7:23 AM, Michael Pearmain wrote:> Hi All, > > I have am using Sweave and the \Sexpr{} to place some numeric > variables in > my tex document. I want to format the number prior to entry so they > read > slightly more elegantly. > Say i have the following numbers > x <- 0.00487324 > y <- 0.000000432 > z <- 0.567 > > I would like to have the numbers displayed as follows > > x1 <- 0.0049 > y1 <- 0.00000043 > z1 <- 0.57 > > I've seen i can use sprintf("%.3f", pi) for example to get the > formating > after the decimal place, but i can't figure out an elegant way to > find the > position of the first non-zero entry to allow me to substitute this > value > into the sprintf command. > > Can anyone offer any advise? > > Thanks in advance > > MikeIt looks like you want the numbers formatted to 2 significant digits, rather than to a fixed number of decimal places. Thus, use: > format(x, digits = 2, scientific = FALSE) [1] "0.0049" > format(y, digits = 2, scientific = FALSE) [1] "0.00000043" > format(z, digits = 2, scientific = FALSE) [1] "0.57" See ?format and note the 'digits' and 'scientific' arguments. HTH, Marc Schwartz