round() for float in C++

后端 未结 22 1667
时光取名叫无心
时光取名叫无心 2020-11-22 03:01

I need a simple floating point rounding function, thus:

double round(double);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1

I can find

22条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 03:31

    It's usually implemented as floor(value + 0.5).

    Edit: and it's probably not called round since there are at least three rounding algorithms I know of: round to zero, round to closest integer, and banker's rounding. You are asking for round to closest integer.

提交回复
热议问题