本文介绍了在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
这篇关于在Go中将float转换为int时如何舍入到最接近的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!