简体   繁体   English

如何在 R 中将字符串双精度转换为数字双精度?

[英]How convert string double into numeric double in R?

I have string "178960.02" that should be converted into numeric with floating-point.我有字符串“178960.02”,应该用浮点数转换为数字。

This generates only integer value "178960" without ".02".这仅生成没有“.02”的整数值“178960”。

How to have double value with floating part如何具有浮动部分的双值

as.numeric("178960.02")
as.double("178960.02")

Thanks!谢谢!

Try this:尝试这个:

#Code
options(digits=9)
as.numeric("178960.02")

Output:输出:

[1] 178960.02

I wondered if the questioner was under the impression that the implicit print of the R REPL (read-eval-print loop) was actually showing the correct value of a double?我想知道提问者是否认为 R REPL(读取-评估-打印循环)的隐式print实际上显示的是双精度值的正确值? (And it appears my worries were well-placed.) (看来我的担忧是有道理的。)

x <- as.numeric("178960.02")

 sprintf("%s", x)
#[1] "178960.02"

print(x, digits = 12)
#[1] 178960.02

The decimal fraction was always there, just not being displayed.小数总是在那里,只是不显示。 It would not necessarily need to have the global options changed to see the more "complete" value.不一定需要更改全局选项才能看到更“完整”的值。 Other options can be set in print arguments.其他选项可以在print参数中设置。

To see that it was always there it is easy to multiply by 100:要看到它始终存在,很容易乘以 100:

x*100
#[1] 17896002

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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