【问题标题】:how to input commas into C++ numeric output [duplicate]如何将逗号输入到C ++数字输出中[重复]
【发布时间】:2012-02-03 16:51:05
【问题描述】:

可能重复:
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?

如何在输出中输入“逗号(,)”。例如:16982 美元,但我需要它看起来像:16,982 美元。

到目前为止,这是我的代码:

#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

}

【问题讨论】:

标签: c++


【解决方案1】:

好吧,您可以尝试将您的 int 转换为字符串,然后按照您想要的方式对其进行格式化...无论如何,您不能直接将逗号插入 int 类型。

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多