0
可能重複:
Is there a built-in function that comma-separates a number in C, C++, or JavaScript?
How do you set the cout locale to insert commas as thousands separators?輸入如何逗號爲C++數字輸出
我怎麼能輸入 「逗號(,)」 我的輸出。例如:16982美元,但我需要它看起來像:16982美元。
這裏是我到目前爲止的代碼:
#include <iostream> // I/O Stream for form
using namespace std; // to prevent redundance of std
int main() // main to form as integer; begins form
{
double housing, food, clothing, transportation, education, healthcare, vacations; // Declaring variables for code below as doubles so you can hold a bigger number; decimal values
// List of expense options that you can choose from
cout << " ----------Yearly Expenses----------" << endl; // outputs heading to user "yearly expenses"
cout << " " << endl; // ends the line and centers the code
cout << " housing $" ; // outputs housing to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " education $" ; // outputs education to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " food $" ; // outputs food to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " clothing $" ; // outputs clothing to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " transportation $" ; // outputs transportation to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " healthcare $" ; // outputs healthcare to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << " vacations $" ; // outputs vacations to show user what expenses they can choose from
cout << " " << endl; // ends the line and centers the code
cout << "\n\n *****Enter your expenses*****" << endl; // outputs heading to user "enter your expenses"
cout << "\n housing: $"; // outputs to user "heading"
cin >> housing; //
cout << "\n education: $"; // outputs to user "education"
cin >> education;
cout << "\n food: $"; // outputs to user "food"
cin >> food;
cout << "\n clothing: $"; // outputs to user "clothing"
cin >> clothing;
cout << "\n transportation: $"; // outputs to user "transportation"
cin >> transportation;
cout << "\n healthcare: $"; // outputs to user "healthcare"
cin >> healthcare;
cout << "\n vacations: $"; // outputs to user "vacations"
cin >> vacations;
double total; // Declaring variable to hold all of the expenses variables
total = (housing + education + food + clothing + transportation + healthcare + vacations); // shows "total" equals all expenses variables added together
cout << "\n\n <><><><>total with 23% tax<><><><>" << endl << endl; // Outputs the heading "total with 23% tax" to user
total = (total * .23) + total; // total equals total multiplied by the 23% to get the percentage tax on your expense
cout << " $" << total << endl; // outputs the total to the user with the added 23% tax
system("pause>nul");
return 0; // returns nothing/0
}
在此處提問並解答:http://stackoverflow.com/questions/7276826/c-format-number-with-commas – Bukes 2012-02-03 16:53:53