user6815493
user6815493

Reputation:

How do I align dollar amounts so cents match up in C++?

My program executes just fine, but I was wondering how to align my output so cents line up rather than dollars.

We've only started class a couple weeks ago, so we haven't gone over this yet. My professor says it's okay for now if they don't align, I guess I'm just OCD about it. Plus, I think it looks a lot cleaner.

Also, if the bill is $38.40, would that be four significant figures? Sorry, I haven't taken math in a while. In my output, I'm getting up to five significant figures for some reason. The most I have is four. How would I fix this, using setprecision?

cout << "Bill \t \t   $  " << bill << endl;
cout << "Tax at 10.5% \t \t $"<<tax<< endl;
cout << "Sub-total \t \t $"<<subTotal<< endl;
cout << "Tip at 20% \t \t $"<<tip<< endl;
cout << endl;
cout << "Total Bill \t \t \t $"<<totalBill<< endl;

As you see, I've been trying it using the tab escape. As a reply suggests, I should use setw?

Edit for 9/10:

I've gotten all my dollar amounts rounded to two decimals, except for the bill, and I don't know how to fix it. Thanks for all the info you've given me, but it's too advanced for what we're doing right now, so I've just aligned things manually. I still need to add setw and then fix everything once that's there. I'm just asking about why the bill is only three digits. It's probably something super simple that's going right over my head.

 // Declare variables
double bill, tax, subTotal, tip, totalBill;

// Variables
bill = 38.40;
tax = .105;
tip = .20;

// Calculate the tax
tax = bill * .105;

// Calculate sub-total of bill
subTotal = bill + tax;

// Calculate tip
tip = subTotal * .20;

// Calculate total amount of bill
totalBill = subTotal + tip;

cout << "Bill" "         $ " << setprecision(4) << bill << endl;
cout << "Tax at 10.5%" " $ " << setprecision(3) << tax << endl;
cout << "Sub-total" "    $ " << setprecision(4) << subTotal << endl;
cout << "Tip at 20%" "   $ " << setprecision(3) << tip << endl;
cout << endl;
cout << "Total Bill" "   $ " << setprecision(4) << totalBill << endl;

Edit: I "fixed" it. All is good now.

Upvotes: 0

Views: 798

Answers (3)

bpeikes
bpeikes

Reputation: 3685

I would do something like:

std::cout << std::setw(15) << std::left << "Bill";
std::cout << std::setw(15) << std::right << std::fixed << std::setprecision(2) << bill << std::endl;

std::cout << std::setw(15) << std::left << "Tax @ 10.5%";
std::cout << std::setw(15) << std::right << std::fixed << std::setprecision(2) << tax << std::endl;

This sets the width of the output for each "column" to 15 characters so you don't have to rely on tabs. All of the the "labels" will be left justified, and all of the prices will be right justified and printed to 2 decimal places. This is a bit more robust than relying on tabs, where you don't have control as to how many characters are used. You can't do proper justification with tabs.

Upvotes: 1

fireant
fireant

Reputation: 14530

One possible way is to use setw.

cout<<setw(5)<<4.55<<endl;
cout<<setw(5)<<44.55<<endl;

output:
 4.55
44.55

Update: as Jonathan Leffler pointed out, the << operator resets the width, hence the code is updated to show it should be repeated.

Upvotes: 1

Trevor Hickey
Trevor Hickey

Reputation: 37806

If you're printing money, I recommend you look at C++'s money I/O.
std::put_money will ensure you are international compliant and printing with correct rounding/precision.

Set the locale of std::cout for USD.
std::showbase will decide whether to print the $.

  //settings for printing as USD
  std::cout.imbue(std::locale("en_US.utf8"));
  std::cout << std::showbase;

Use std::setw and std::left for formatting.
Here is an example of printing your data:

#include <iostream>
#include <string>
#include <iomanip>  

//row data from example
struct Row{
  std::string description;
  float amount;
};

//function for printing a row
void Print(Row row);

int main(){

  //example rows
  Row a{"Bill",3840};
  Row b{"Tax at 10.5%",403};
  Row c{"Sub-total",4243};
  Row d{"Tip at 20%",848};
  Row e{"Total Bill",5091};

  //settings for printing as USD
  std::cout.imbue(std::locale("en_US.utf8"));
  std::cout << std::showbase;

  //format printing
  Print(a);
  Print(b);
  Print(c);
  Print(d);
  std::cout << '\n';
  Print(e);
}

void Print(Row row){
  static const int COLUMN_WIDTH{14};
  std::cout << std::setw(COLUMN_WIDTH) << std::left << row.description;
  std::cout << " " << std::right << std::put_money(row.amount) << '\n';
}

result:

Bill           $38.40
Tax at 10.5%   $4.03
Sub-total      $42.43
Tip at 20%     $8.48

Total Bill     $50.91

Upvotes: 4

Related Questions