我想计算一个简单的数字,如果这个数字不是整数我想把它四舍五入。
例如,如果经过计算得到1.2
,我想将其更改为2
。如果数字是3.7
,我想把它改成4
等等。
最佳答案
您可以使用 math.ceil
将 Double
向上舍入并使用 toInt
将 Double
转换为整数
。
def roundUp(d: Double) = math.ceil(d).toInt
roundUp(1.2) // Int = 2
roundUp(3.7) // Int = 4
roundUp(5) // Int = 5
关于scala - 如果它不是整数,如何四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298393/