在Go中将float转换为int时如何舍入到最接近的int

在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