使用 round() 在 Swift 中舍入 [英] Rounding in Swift with round()
本文介绍了使用 round() 在 Swift 中舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在玩的过程中,我很快发现了 round() 函数.可以如下使用:
While playing around, I found the round() function in swift. It can be used as below:
round(0.8)
正如预期的那样,它将返回 1.这是我的问题:
Which will return 1, as expected. Here's my question:
我如何快速四舍五入?
我希望能够插入一个数字,比如 0.6849,然后返回 0.685.round() 如何做到这一点?或者,它不是,在这种情况下,什么功能呢?
I want to be able to plug in a number, say 0.6849, and get 0.685 back. How does round() do this? Or, does it not, in which case, what function does?
推荐答案
您可以:
round(1000 * x) / 1000
这篇关于使用 round() 在 Swift 中舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文