round() for float in C++

后端 未结 22 1221
时光取名叫无心
时光取名叫无心 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:25

    It may be worth noting that if you wanted an integer result from the rounding you don't need to pass it through either ceil or floor. I.e.,

    int round_int( double r ) {
        return (r > 0.0) ? (r + 0.5) : (r - 0.5); 
    }
    

提交回复
热议问题