0

I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places.

#include <iostream.h>
#include <cstdlib.h>
#include <string.h>

int main() {
    float sale_price;
    float tax_rate;
    float discount_rate;

    system("cls");
    system("color 07");
    cout << "\n\nWelcome to the second version of my total purchase price calculator!\n";
    cout << "How much did your recently purchased item cost? ";
    cin >> sale_price;
    cout << "What is the tax rate in your area? ";
    cin >> tax_rate;
    cout << "What was the discount rate, if any (if none, just put down 1) ";
    cin >> discount_rate;
    float tax = sale_price*(tax_rate/100);
    float discount = sale_price*(discount_rate/100);
    float total_price = sale_price + tax - discount;
    cout << "The total price of your item is $"<<total_price<<" with $"<<tax<<" tax minus $"<<discount<<" due to discount.\n";
    cout << "Would you like a reciept? y or n. ";
    string answer;
    End:
    cin >> answer;
    if (answer == "y") {
        goto Reciept;
    }
    else if (answer == "n") {
        return 0;
    }
    else {
        cout << "Try another answer\n";
        goto End;
    }
    Reciept:
    system("cls");
    system("color 70");
    cout << "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";
}

this usually gives me 4 decimal places just FYI

apaderno
  • 28,547
  • 16
  • 75
  • 90
Allir
  • 1
  • 3

5 Answers5

3

Round to 2 decimals: int((n * 100.0) + 0.5) / 100;
Round to 3 decimals: int((n * 1000.0) + 0.5) / 1000;
etc.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Dabbler
  • 9,733
  • 5
  • 41
  • 64
2

The easy way is to use setprecision:

 std::cout << "The total price of your item is $"
           << std::setprecision(2) << total_price ...

This will occasionally get things wrong. A better solution is to use an improved rounding function that is not a part of the standard (e.g., How does Excel successfully Rounds Floating numbers even though they are imprecise?). An even better solution is to implement your own fixed point arithmetic algorithm so as to avoid this problem entirely. You can still go one better than that: Use a fixed point arithmetic package that someone else has already written and tested to the nth degree.

Community
  • 1
  • 1
David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • "Fixed Point" does not mean fixed base-10 precision. It means fixed binary point representation. That would not help here. – Chriszuma Oct 05 '11 at 18:39
  • Fixed Point can be done in base-10. http://en.wikipedia.org/wiki/Fixed-point_arithmetic "The two most common fixed-point types are decimal and binary." – Mooing Duck Oct 05 '11 at 18:43
  • @Chriszuma: What gives you that idea? Fixed point can mean many things. It most certainly is not restricted to binary, and when it comes to dollars and cents, it almost certainly is restricted to base 10. Particularly, 1/100. Ada, Cobol, and lots of other languages offer direct support for base 10 fixed point arithmetic. C/C++ do not. Various TRs have been proposed and later rejected/withdrawn to provide such support to C and to C++. It's not hard; there are lots of public domain fixed point packages. Or just read Dr. Dobbs. – David Hammen Oct 05 '11 at 18:52
  • I guess I am just a C programmer at heart. I didn't know such things existed :) – Chriszuma Oct 05 '11 at 18:53
  • I'm removing my prior comment since it seems I just don't get out enough. The Dr. Dobbs suggestion was enlightening. – Mark Ransom Oct 05 '11 at 18:56
2

You should use Iomanip's setprecision:

setprecision reference

marcocamejo
  • 798
  • 1
  • 7
  • 20
1

When you output with cout, use the setprecision() operator.

cout << fixed;
cout << setprecision(2) << sale_price*(tax_rate/100);
Chriszuma
  • 4,464
  • 22
  • 19
  • 2
    This might result in odd output if the discount and tax round differently to the total. You need the rounding to occur on the actual numbers prior to totalling. – Mark Ransom Oct 05 '11 at 18:33
  • I think it is better to be more precise throughout than to avoid confusing pennies popping up / disappearing. – Chriszuma Oct 05 '11 at 18:36
  • But that's exactly what will happen with this simple solution - the pennies won't add up properly. Not all the time, anyway. – Mark Ransom Oct 05 '11 at 18:40
  • That's my point. I think having the pennies add up properly is less important than keeping full precision throughout. Otherwise you might be losing pennies on every transaction. – Chriszuma Oct 05 '11 at 18:43
  • 1
    Your priorities are not shared very widely when it comes to money. If you need higher precision you might display fractions of a penny instead of whole pennies, but in the end they must all add up. – Mark Ransom Oct 05 '11 at 18:50
1

Do

cout.precision(2);
cout << fixed;

before you output the floats.

kralyk
  • 4,249
  • 1
  • 32
  • 34