如何四舍五入到数字的下一个 50?

How can I round to the next 50 of a number?

我正在调试我的工资计算器,我希望将一个变量四舍五入到最接近的 50,但不包括 100。

例如,我有变量 23324.60,我需要一个等于 23350.00 的公式。

这是为了满足以下关于 AR 预扣税计算的说明。

"Since the Net Taxable Income is less than 50,000, we will take the income to the midrange of ,350.00 (midrange of ,300.00 and ,400.00). "

因为您四舍五入到 50 的每个奇数倍数,所以您可以将其视为 向下 舍入到完整的 100 并在之后加上 50。例如,这将使 200 和 299.99 都四舍五入为 250

我基于这种方法的解决方案是:

double rounded_income(double income) {
    return 50.0 + 100.0 * floor(income / 100.0);
}

The floor function<cmath>header中提供。另一种方法是在整数类型之间来回转换,但会有很多缺点,包括更差的可读性。

#include <iostream>
#include <math.h>

using namespace std;

int getRoundUp50(float value)
{
    return ceil(value * 0.02) * 50;
}

int main()
{
    cout << getRoundUp50(120.5) << endl;
    cout << getRoundUp50(125.0) << endl;

    return 0;
}

结果:

150
150