c++ - Round a float number to the nearest integer divisible by five -
i search everywhere, no solution , need round nearest 5 integers, don't know how formulate , example round(0.13) should return 5 ; here pattern logic value round first , expected result after rounding ;
0.12 => 0 0.99 => 0 1.01 => 0 4.99 => 5 5.45 => 5 7.00 => 5 8.00 => 10 9.10 => 10 14.34 => 15 17.4 => 15 17.5 => 20 37.6 => 40
try
float x = roundf(x / 5) * 5;
or, assuming x >= 0
(and, @jameskanze noted, x <= int_max
)
int n = (int)(roundf(x / 5) * 5 + 0.5);
Comments
Post a Comment