go中没有对float64进行四舍五入的函数,
只有:
math.Floor
:向下取整
math.Ceil
:向上取整
但可以用以下方法达到四舍五入的效果
func Round(v float64) int {
return int(math.Floor(v + 0.5))
}
单测:
func TestRound(t *testing.T) {
assert.Equal(t, 4, Round(4.1))
assert.Equal(t, 5, Round(4.5))
}
结果:
4
5