如何做“漂亮的四舍五入"? [英] How to make "pretty rounding"?

查看:37
本文介绍了如何做“漂亮的四舍五入"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做这样的舍入并将其转换为字符:

I need to do a rounding like this and convert it as a character:

as.character(round(5.9999,2))

我希望它变成 6.00,但它只给了我 6

I expect it to become 6.00, but it just gives me 6

无论如何我可以让它显示6.00?

Is there anyway that I can make it show 6.00?

推荐答案

尝试以下任一方法:

> sprintf("%3.2f", round(5.9999, digits=2))
[1] "6.00
> sprintf("%3.2f", 5.999)  # no round needed either
[1] "6.00

还有formatC()prettyNum().

这篇关于如何做“漂亮的四舍五入"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆