我有一个数字,例如1.128347132904321674821,我想在输出到屏幕(或写入到文件)时仅显示两个小数点后的位置。要怎么做呢?

x <- 1.128347132904321674821

编辑:

用途:

options(digits=2)

被认为是一个可能的答案。是否有一种方法可以在脚本中指定这一点,以便一次性使用?当我将它添加到我的脚本时,它似乎没有做任何不同的事情,我对大量重新输入来格式化每个数字不感兴趣(我正在自动化一个非常大的报告)。

--

答案:整数(x,数字=2)


当前回答

Background: Some answers suggested on this page (e.g., signif, options(digits=...)) do not guarantee that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of "significant figures". However, in many domains (e.g., APA style, business reports) formatting requirements dictate that a certain number of decimal places are displayed. This is often done for consistency and standardisation purposes rather than being concerned with significant figures.

解决方案:

下面的代码显示了数字x的小数点后两位。

format(round(x, 2), nsmall = 2)

例如:

format(round(1.20, 2), nsmall = 2)
# [1] "1.20"
format(round(1, 2), nsmall = 2)
# [1] "1.00"
format(round(1.1234, 2), nsmall = 2)
# [1] "1.12"

一个更一般的函数如下所示,其中x是数字,k是要显示的小数的数量。Trimws删除任何前导空白,如果你有一个数字向量,这可能是有用的。

specify_decimal <- function(x, k) trimws(format(round(x, k), nsmall=k))

例如,

specify_decimal(1234, 5)
# [1] "1234.00000"
specify_decimal(0.1234, 5)
# [1] "0.12340"

替代方案的讨论:

formatC答案和sprintf答案工作得相当好。但在某些情况下,它们会显示负0,这可能是不需要的。也就是说,

formatC(c(-0.001), digits = 2, format = "f")
# [1] "-0.00"
sprintf(-0.001, fmt = '%#.2f')
# [1] "-0.00"

一个可能的解决方法如下:

formatC(as.numeric(as.character(round(-.001, 2))), digits = 2, format = "f")
# [1] "0.00" 

其他回答

Background: Some answers suggested on this page (e.g., signif, options(digits=...)) do not guarantee that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of "significant figures". However, in many domains (e.g., APA style, business reports) formatting requirements dictate that a certain number of decimal places are displayed. This is often done for consistency and standardisation purposes rather than being concerned with significant figures.

解决方案:

下面的代码显示了数字x的小数点后两位。

format(round(x, 2), nsmall = 2)

例如:

format(round(1.20, 2), nsmall = 2)
# [1] "1.20"
format(round(1, 2), nsmall = 2)
# [1] "1.00"
format(round(1.1234, 2), nsmall = 2)
# [1] "1.12"

一个更一般的函数如下所示,其中x是数字,k是要显示的小数的数量。Trimws删除任何前导空白,如果你有一个数字向量,这可能是有用的。

specify_decimal <- function(x, k) trimws(format(round(x, k), nsmall=k))

例如,

specify_decimal(1234, 5)
# [1] "1234.00000"
specify_decimal(0.1234, 5)
# [1] "0.12340"

替代方案的讨论:

formatC答案和sprintf答案工作得相当好。但在某些情况下,它们会显示负0,这可能是不需要的。也就是说,

formatC(c(-0.001), digits = 2, format = "f")
# [1] "-0.00"
sprintf(-0.001, fmt = '%#.2f')
# [1] "-0.00"

一个可能的解决方法如下:

formatC(as.numeric(as.character(round(-.001, 2))), digits = 2, format = "f")
# [1] "0.00" 

小数点后两位假设你想保持后面的0

sprintf(5.5, fmt = '%#.2f')

这给了

[1] "5.50"

正如下面@mpag提到的,似乎R有时会给出意想不到的值,而舍入方法,例如sprintf(5.5550, fmt='%#.2f')给出5.55,而不是5.56

这是我从单位到百万的方法。 Digits参数让我调整有效值(整数+小数)的最小数量。你可以先调整里面的十进制四舍五入。

number <-function(number){
  result <- if_else(
    abs(number) < 1000000,
    format(
      number, digits = 3,
      big.mark = ".",
      decimal.mark = ","
    ),
    paste0(
      format(
        number/1000000,
        digits = 3,
        drop0trailing = TRUE,
        big.mark = ".",
        decimal.mark = ","
      ),
      "MM"
    )
  )
  # result <- paste0("$", result)
  return(result)
}

请注意,R中的数字对象以双重精度存储,这给了你(大约)16个十进制数字的精度-其余的将是噪声。我承认上面显示的数字可能只是一个例子,但它有22位数长。

如果你更喜欢有效数字而不是固定数字,那么signif命令可能会有用:

> signif(1.12345, digits = 3)
[1] 1.12
> signif(12.12345, digits = 3)
[1] 12.1
> signif(12345.12345, digits = 3)
[1] 12300