R 中将数字四舍五入到小数点后 2 位的常用代码是:
> a = 14.1234
> round(a, digits=2)
> a
> 14.12
但是,如果该数字的前两位小数位为零,则 R 会在显示中抑制零:
> a = 14.0034
> round(a, digits=2)
> a
> 14
我们怎样才能让 R 显示第一个小数位,即使它们为零?我在情节中特别需要这个。我在这里搜索过,有些人建议使用options(digits=2)
,但这使得 R 有一种奇怪的行为。
我们可以用format
format(round(a), nsmall = 2)
#[1] "14.00"
正如 @arvi1000 在评论中提到的,我们可能需要指定digits
in round
format(round(a, digits=2), nsmall = 2)
data
a <- 14.0034
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)