1

I'm building a calculator application that will calculate the exact net pay after taxes. The problem I am having is that the program automatically rounds the values up.

I tried using setprecision, but it seems to only work when used in a cout or cin line. That would require user input, the values I need to apply setprecision on are determined by the program itself. For instance, if I place setprecision on line 42, which updates the variable 'federal', the compiler returns an error expecting a ';' prior to it. How can I set the precision of a variable without requiring user input.

To clarify, the only variables which the user enters are 'hours' and 'payrate'. The other variables are interacted with like the example below.

40 if ((gross >= 0) && (gross < 260))
41     {
42         federal = ((gross - 73) * .1);
43     }
  • More code context please. – drescherjm Apr 30 '19 at 03:14
  • Please provide a [mcve]. – L. F. Apr 30 '19 at 03:25
  • 1
    Possible duplicate of [Changing number precision](https://stackoverflow.com/questions/3069950/changing-number-precision) – kmdreko Apr 30 '19 at 07:42
  • What is the type of `gross`? If it's a floating point type, you may consider functions like [`std::round`](https://en.cppreference.com/w/cpp/numeric/math/round) or [`std::ceil`](https://en.cppreference.com/w/cpp/numeric/math/ceil). [`std::setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision) is an input/output manipulator which doesn't affect the value of your variable, but only how it is outputted, e.g. in `std::cout << std::setprecision(100) << gross << '\n';`. – Bob__ Apr 30 '19 at 08:57

1 Answers1

0

I actually figured out my mistake here. I was declaring the variables at integers, forgetting integers are whole numbers, so I changed it to floats, and that fixed it.