doucheng1063 2016-01-31 15:59 采纳率: 100%
浏览 162
已采纳

在Go中将float转换为int时如何舍入到最接近的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) should equal 4 instead of 3.

  • 写回答

1条回答 默认 最新

  • duangeli1334 2016-01-31 15:59
    关注

    int(f+0.5) will cause for it to round upwards if it's >= .5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?