简体   繁体   中英

A version of as.character that does not round integers

base::as.character() rounds integers with decimal places:

>as.character(5.00)
>"5"

So the two zeros after the decimal place are lost, whereas

>as.character(5.01)
>"5.01"

Keeps the two numbers. Although I can understand whilst dropping the zeros is sensible, I'm interested in where unnecessary digits have been used, so I'd like to keep everything. What I really want is something like:

>as.character.f(c(5, 5.0, 5.00))
>"5"  "5.0"  "5.00" 

I guess you could use more of a for-loop to be able to reproduce what you want:

mapply(format,c(5,5.0,5.00),nsmall=c(0,1,2))
[1] "5"   "5.0"  "5.00"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM