我该如何将一个数字的下一个50转

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

本文关键字:一个 数字 50转 下一个      更新时间:2023-10-16

我正在努力调试我的工资计算器,我想将变量舍入到最近的50个,但不包括100。

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

这是要符合以下有关AR税收计算的指令。

"由于净应税收入少于50,000,我们将收入达到$ 50的中股(23,350.00美元((中额为23,300.00美元和$ 23,400.00(。"

,因为您可以将每一个零件绕过50个奇数,所以您可以将其视为圆形 down 到完整的100s,然后添加50。例如,这将使200和299.99均投向250

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

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

floor功能在<cmath>标头中提供。另一种方法是从整数类型中来回走动,但会有许多缺点,包括更糟糕的可读性。

#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