I am wondering if there is a way to write 0.000523 in scientific notation ( 5.23x10-4
) in R
. I am using the xtable
command to write a table into latex
.
You can use sprintf
like this:
x <- c(0.000523, -523)
sprintf("%.2fx10^{%d}",
x/10^floor(log10(abs(x))),
floor(log10(abs(x))))
#[1] "5.23x10^{-4}" "-5.23x10^{2}"
You'll need to write some ifelse
conditions to have different formatting depending on the decimal ranges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With