[英]Why does as.character drop decimal point?
I'm interested to know why as.character(5.0)
returns 5
but as.character(5.1)
returns 5.1
in R.我很想知道为什么
as.character(5.0)
返回5
但as.character(5.1)
在 R 中返回5.1
。 I tried to get an answer by reading the documentation but had no luck.我试图通过阅读文档来获得答案,但没有运气。
I'm interested to know why as.character(5.0) returns 5
我很想知道为什么 as.character(5.0) 返回 5
The key word here is "returns ."这里的关键词是“回报”。 What do you mean by that?
你是什么意思? Note that typing this in the console gives you 5:
请注意,在控制台中输入此命令会给您 5:
> 5.0
[1] 5
5 is the same things as 5.0 for the purposes of calculation.为了计算的目的,5 与 5.0 相同。 So what you probably really care about is how 5 is printed .
所以你可能真正关心的是如何打印5 。 You thus need to use joran's method or a function like
sprintf
.因此,您需要使用 joran 的方法或像
sprintf
这样的 function 。
For more precise formatting of numbers as characters, you might use format
:要更精确地将数字格式化为字符,您可以使用
format
:
> format(5,nsmall = 1)
[1] "5.0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.