-3

I am working on debugging my payroll calculator, and I want to have a variable rounded to the nearest 50, but not including 100.

For instance, I have the variable 23324.60, and I need a formula that equates to 23350.00.

This is to meet the following instruction on AR Tax Withholding calculation.

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

  • There are very few lines of code that you show – Ted Lyngmo May 03 '19 at 23:02
  • 1
    @TedLyngmo Zero (efforts) actually. – πάντα ῥεῖ May 03 '19 at 23:07
  • So, divide by 50, round, multiply by 50 - it's not rocket science. Oh - hang on- you said not 100. What kind of maths is this? I give up. – James Gaunt May 03 '19 at 23:09
  • BTW: using floating point to represent currency values is not a good idea, since float point variables cannot represent values like `0.1` or `0.01` exactly (such values cannot be represented as a base-2 fraction). The usual way of representing currency is with integral values (e.g. an integer representing number of cents, or a structure containing two integral values - one which represents dollars and the other that represents cents). – Peter May 04 '19 at 02:09

2 Answers2

1

Because you round to every odd multiple of 50, you can think of it as rounding down to full 100s and adding 50 after that. For example this would make 200 and 299.99 both round to 250

My solution based on this approach would be:

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

The floor function is provided in <cmath> header. An alternative way would be to do casting back and forth to and from integer types, but would have many disadvantages including much worse readability.

Maarrk
  • 41
  • 4
-1
#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;
}

Result:

150
150
Tony
  • 632
  • 1
  • 4
  • 18