go - 在将float转换为int时如何舍入到最近的int [英] How to round to nearest int when casting float to int in go
本文介绍了go - 在将float转换为int时如何舍入到最近的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当将 float 转换为 int 时,小数会被丢弃.什么是干净的转换方式,以便它四舍五入到最接近的整数.
When casting float to int the decimal is discarded. What's a clean way to cast so that it rounds to the nearest whole number instead.
x := int(3.6)
应该等于 4 而不是 3.
x := int(3.6)
should equal 4 instead of 3.
推荐答案
int(f+0.5)
将导致它向上舍入如果 >= .5
int(f+0.5)
will cause for it to round upwards if it's >= .5
这篇关于go - 在将float转换为int时如何舍入到最近的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文