R floating point number precision being lost on coversion from character

天涯浪子 提交于 2019-12-19 08:54:13

问题


I have a large floating point number as a character like so

  x<-"5374761693.91823";

On doing

 as.numeric(x); 

I get the following output

   5374761694

I would like to preserve the floating point nature of the number while casting.


回答1:


use digits argument in print to see the actual number:

> print(as.numeric(x), digits=15)
[1] 5374761693.91823

options is another alternative:

> options(digits=16)
> as.numeric(x)
[1] 5374761693.91823

> # assignments
> options(digits=16)
> y <- as.numeric(x)
> y
[1] 5374761693.91823

z <- print(as.numeric(x), digits=15)
z


来源:https://stackoverflow.com/questions/17878703/r-floating-point-number-precision-being-lost-on-coversion-from-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!