【问题标题】:Add integer to a float and get the output as float in r [duplicate]将整数添加到浮点数并将输出作为浮点数在 r [重复]
【发布时间】:2022-01-14 19:11:17
【问题描述】:
x <- 1626307200
y <- 0.034

x+y

我想在 r 中添加这两个数字并将其保存为浮点数 (1626307200.034)。 这两个数字都是数字格式。 试了好几种方法都不管用

【问题讨论】:

  • 结果仍然是double类型。 R 打印最大为getOption("digits") 有效数字的数字。您可以使用例如options(digits = 16) 来增加此最大值。现在 R 将 x + y 打印为 1626307200.034
  • 您可以使用sprintf("%.3f",x+y)查看值。
  • 只存储z &lt;- x+y z 是正确的值。可能你想知道你看不到它。只知道z!=x。你也可以得到z的小数部分,即z%%1
  • 您也可以直接在print() 中使用digits 参数,而无需更改全局选项,例如print(x + y, digits = 15)

标签: r floating-point numeric digits output-formatting


【解决方案1】:

有一个命令可以让R显示更多的数字:

options(digits = 15)
x <- 1626307200
y <- 0.034

z = x + y
z # 1626307200.034

【讨论】:

    猜你喜欢
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多