0

Sorry if my title was vague, im learning c++ and we're making a checkout/inventory system which calculates currency, if there is any change such as 1.69, it should automatically round up to 1.70 or if it was 1.63 it would round up to 1.65, avoiding giving change in cents.

Heres my relevant code that im attempting to adapt to return the correct rounding:

if (!taxed()) {
                    cout << price() * quantity();

                }
                else {
                    cout <<  (price() * quantity()) * 1.13;
                }

Please note that i attempted cout precision with no success.

I managed to output to 1.7, but how do you change it to 1.70?

Tried changing the precision but that changed my answer, also setting it to double etc didnt help.

petermytt
  • 1
  • 3

1 Answers1

0

Perhaps the easiest way:

  1. Multiply by 100 (to get wanted value without decimals),
  2. Divide by 5 (because that's the precision you want),
  3. Round this number. Now reverse the procedure with rounded number:
  4. Multiply by 5 and
  5. Divide by 100 (to get value with two decimal places).

Note that you can shorten the procedure:

  1. Multiply by 20 (previous steps 1 and 2 combined),
  2. Round the number,
  3. Multiply by 0.05.

The main thing is dividing by wanted precision (5) and rounding it then.

fbxmg
  • 146
  • 3
  • How do you "round the number" – petermytt Dec 09 '15 at 09:02
  • Also, multiplying 1.69 by 100 gives me 169, dividing it by 5 gives me 33.8... Multiplying by 5 then dividing by 100 gives me 1.69 again. – petermytt Dec 09 '15 at 09:05
  • Depending on the version you use, you might not have round() function available. How to do it in that case, you can find some examples in https://stackoverflow.com/questions/485525/round-for-float-in-c – fbxmg Dec 09 '15 at 09:07
  • About your second comment - that's why you have to round the number. So when you get 33.8, it will be rounded to 34. After multiplying it by 5 (and dividing by 100 then) you get 1.70. – fbxmg Dec 09 '15 at 09:08
  • Not sure how to implement that link into my code. im using g++ compiler so its nifty. – petermytt Dec 09 '15 at 09:08
  • I managed to output to 1.7 but thats by setting the precision to 1. How can i change it to 1.70? – petermytt Dec 09 '15 at 09:09
  • Use fixed precision and 2 decimal places: https://stackoverflow.com/questions/22515592/how-to-use-setprecision-in-c – fbxmg Dec 09 '15 at 09:17
  • without adding 0.05 at the start? – petermytt Dec 09 '15 at 09:20
  • Yes, without adding. Use this adding only for rounding, if the round() function is not available to you. So if you call your determined price (with quantity and tax) qtPrice, solution would either look like round(qtPrice*20)*0.05 or e.g. floor(qtPrice*20 + 0.05)*0.05 – fbxmg Dec 09 '15 at 09:23
  • No way to avoid using round? i dont think it will work with my version. – petermytt Dec 09 '15 at 09:30
  • Setting my precision to 1 lets it be 1.7, setting it to 2 puts it back at 1.69. I want 1.70 – petermytt Dec 09 '15 at 09:34
  • It can be 1.69 only if you are not rounding it. There has to be rounding at some point, if you want the value to be rounded to nearest .05. Sure, you can also do a number of if-then-else cases to cover everything, but that is not really needed. If round doesn't work in your version, floor definitely is, so use cout << floor(qtPrice*20 + 0.05)*0.05, where qtPrice is your starting price. – fbxmg Dec 09 '15 at 09:41
  • Is there any functions or includes? http://puu.sh/lOYrX/7bc9c0ec48.png doesnt fix it. Output still 1.69.... Price() returns the starting price. – petermytt Dec 09 '15 at 09:46
  • By qtPrice I didn't mean starting price of the product, but starting price of this rounding procedure. :) So your total should include floor(price()*quantity()*1.13(if taxed)*20+0.05)*0.05 – fbxmg Dec 09 '15 at 09:50
  • it rounded it down, from 1.69 to 1.65 – petermytt Dec 09 '15 at 09:54
  • I think if i just add 0.05 to the total it would fix itself. – petermytt Dec 09 '15 at 09:59
  • Oh, yes, sorry, what a stupid mistake on my part... When rounding with floor, you should add 0.5 in this case, not 0.05, as you're rounding to whole number. So use floor(price()*quantity()*1.13(if taxed)*20+0.5)*0.05 and that should finally work. – fbxmg Dec 09 '15 at 10:00
  • Compiling this on g++ says i have not declared floor. are you sure there isnt any declarations for it? – petermytt Dec 09 '15 at 10:11
  • If it doesn't compile, try to use #include . If it still won't work, you could perhaps try to reference a library while compiling. – fbxmg Dec 09 '15 at 10:18