2012-02-03 4 views
0

Possible en double:
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?comment virgules entrée en sortie numérique C de

Comment je pouvais entrée "virgules (,)" à mes sorties. par exemple: 16982 $ mais j'en ai besoin: 16 982 $.

Voici mon code à ce jour:

#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 

} 
+0

Demandé et répondu ici: http://stackoverflow.com/questions/7276826/c-format-number-with-commas – Bukes

Répondre

0

Eh bien, vous pouvez essayer de convertir votre int en une chaîne puis formatez la façon dont vous voulez ... Quoi qu'il en soit que vous venez de virgules catifs insérer dans un type int directement.