将浮点数四舍五入为最接近的可被五整除的整数

Round a float number to the nearest integer divisible by five

我到处找这个,没有解决方案,我需要四舍五入到最接近的5个整数, 不知道如何表述这个,例如 round(0.13) 应该 return 5 ;这是模式逻辑,值先舍入,舍入后预期结果;

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

尝试

float x = roundf(x / 5) * 5;

或者,假设 x >= 0(并且,正如 @JamesKanze 指出的,x <= INT_MAX

int n = (int)(roundf(x / 5) * 5 + 0.5);

尝试5 * floor ((n + 2.5) \ 5),其中\表示整数除法。这显然不是代码,但可以轻松翻译成您喜欢的任何语言。