ThePyroMark
ThePyroMark

Reputation: 45

How can I make my answer as a fixed decimal dollar amount in c++?

Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed.

cout << "You have made $" << TotalSales <<" dollars from ticket sales!!";

Upvotes: 0

Views: 2290

Answers (2)

Fred Larson
Fred Larson

Reputation: 62063

You could use std::put_money:

std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "You have made "
    << std::showbase << std::put_money(TotalSales * 100.0)
    << " from ticket sales!!";

This will automatically apply decimal and thousands separators. It will also add the currency symbol if you use the showbase manipulator. The formatting of the currency value will depend on the locale you provide.

Upvotes: 2

Sam12345
Sam12345

Reputation: 91

std::setprecision() as well as std::fixed are the functions you want:

std::cout <<std::fixed <<std::setprecision(2) <<"You have made $" <<TotalSales <<" dollars from ticket sales!!";

Upvotes: 1

Related Questions