舍入数字并强制 R 显示 0

标签 r decimal rounding

我正在计算值,这些值本质上是十进制值,并且还应该在表格中报告它们。为了简洁起见,我如何将所有值四舍五入到小数点后 3 位,甚至强制 R 为小数点后 3 位以下的值添加 0?这是一个更实际的例子:

# The following vector contains all the values I computed (this is an example)
results <- c(1.25854, 3.2, 2.84)

# I am currently using the round() function, which does not give me the desired result
rounded_results <- round(results, 3)
rounded_results

> [1] 1.259 3.2 2.84

# The result I would like to obtain is

> [1] 1.259 3.200 2.840

最佳答案

这是我的方法:

results <- c(1.25854, 3.2, 2.84)
options(digits=4)

> results
[1] 1.259 3.200 2.840

关于舍入数字并强制 R 显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26448050/

相关文章:

r - R中的图像比较

r - getSymbols 并使用 lapply、Cl 和 merge 提取收盘价

r - 为什么 R 的 ccf 说 "Autocorrelations of series ' X',延迟”?

r - createDataFrame 中的 SparkR 瓶颈?

python十进制上下文输出

c# - 小数截断问题

javascript - JS 智能舍入小数

python 3.3.2 我对函数 "round"有正确的理解吗?

Python 自动舍入小数

ios - 带逗号小数点分隔符的 NSNumberFormatter