round() for float in C++

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

    Best way to rounding off a floating value by "n" decimal places, is as following with in O(1) time:-

    We have to round off the value by 3 places i.e. n=3.So,

    float a=47.8732355;
    printf("%.3f",a);
    

提交回复
热议问题