逗号格式化C ++中的数字

时间:2013-03-20 01:32:31

标签: c++

我需要帮助为用户输入的数字添加逗号,一些指导或帮助将不胜感激。到目前为止,我有它存储前三位数字和后六位数字,然后简单格式化。

#include<iostream>

using namespace std;
int main ( int argc, char * argv[] ) 
{
unsigned long long userInput;
int fthreeDigit;

cout << "Enter a long long number: " << endl;
cin >> userInput;

fthreeDigit = ( userInput / 1000 );
userInput %= 1000;

cout << "Your Number: " << fthreeDigit << "," << userInput << endl;
system("pause");
return 0;
 }

5 个答案:

答案 0 :(得分:9)

这是你需要的吗?语言环境将正确地为您完成此操作。

#include <iostream>
using namespace std;

int main ( int argc, char * argv[] ) 
{
  unsigned long long userInput;
  int fthreeDigit;
  cout << "Enter a long long number: " << endl;
  cin >> userInput;
  std::cout.imbue(std::locale(""));
  std::cout << userInput << std::endl;

  return 0;
}

答案 1 :(得分:2)

编辑: 我有两个解决方案。首先不使用数字(推荐)和第二(分组)。 第一个解决方案是:

#include <cstdlib>
#include <iostream>
#include <locale>
#include <string>
using namespace std;

struct my_facet : public std::numpunct<char>{
        explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
        virtual char do_thousands_sep() const { return ','; }
        virtual std::string do_grouping() const { return "\003"; }
};

/*
 * 
 */
int main(int argc, char** argv) {

    cout<<"before. number 5000000: "<<5000000<<endl;

    std::locale global;
    std::locale withgroupings(global, new my_facet);
    std::locale was = std::cout.imbue(withgroupings);

    cout<<"after. number 5000000: "<<5000000<<endl;

    std::cout.imbue(was);

    cout<<"and again as before. number 5000000: "<<5000000<<endl;

    return 0;
}
  
    

之前。数字5000000:5000000
    后。数字5000000:5,000,000

  
     和以前一样。数字5000000:5000000

     

RUN SUCCESSFUL(总时间:54ms)

和第二个(不推荐)是:

double f = 23.43;
std::string f_str = std::to_string(f);

或者

int a = 1;
stringstream ss;
ss << a;
string str = ss.str();

然后,您可以使用string::substr() string::find() string::find_first_of()和类似方法修改和格式化字符串。
a similar topic


如果你真的想要(必须)划分:(我认为我的版本比其他版本更清晰,更有效率)

unsigned long long userInput;
    std::stringstream ss,s0;
    std::string nr;
        std::cout << "Enter a long long number: " << std::endl;
        std::cin >> userInput;
        int input=userInput;
        int digits;

        while(input>999){
            input=input/1000;
            digits=userInput-input*1000;
            int mdigits=digits;
            while(mdigits<100){s0<<"0";mdigits*=10;}
            std::string s=ss.str();
            ss.str("");
            ss<<","<<s0.str()<<digits<<s;
            userInput=input;
            s0.str("");
        }

        std::string sf=ss.str();
        ss.str("");
        ss<<input<<sf;

        std::cout << "Your Number: " << userInput << ";" << digits <<";"<<ss.str()<<std::endl;
  

输入长号:12345678您的号码:12; 345; 12,345,678

答案 2 :(得分:0)

这是蛮力但可能最容易理解的方法是在矢量的帮助下获得每千个数字。

#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ( int argc, char * argv[] ) 
{
    long long userInput;
    int fthreeDigit;

   cout << "Enter a long long number: " << endl;
   cin >> userInput;
   vector <int> res;  //use vector to store every 3 digits
   while (userInput !=0)
   {
     fthreeDigit = userInput %1000;
     res.push_back(fthreeDigit);
     userInput = userInput / 1000 ;
   }

   std::reverse(res.begin(), res.end());
   for (size_t i = 0; i < res.size()-1; ++i)
   {
     if (res[i] ==0)
     {
        cout << "000"<<",";
     }
     else
     {
       cout << res[i] << ",";
     }
   }

   if (res[res.size()-1] == 0)
   {
       cout << "000";
   }
   else{
       cout << res[res.size()-1];
   }
   cout <<endl;
   cin.get();
   return 0;
}

我使用以下案例测试了此代码:

Input: 123456 Output: 123,456
Input: 12     Output: 12
Input: 12345  Output: 12,345
Input: 1234567 Output: 1,234,567
Input: 123456789 Output: 123,456,789
Input: 12345678 Output: 12,345,678

根据您对评论的回应,我想这就是您想要的。

答案 3 :(得分:0)

你可以这样做:

#include <iostream>
#include <string>

using namespace std;

string commify(unsigned long long n)
{
  string s;
  int cnt = 0;
  do
  {
    s.insert(0, 1, char('0' + n % 10));
    n /= 10;
    if (++cnt == 3 && n)
    {
      s.insert(0, 1, ',');
      cnt = 0;
    }
  } while (n);
  return s;
}

int main()
{
  cout << commify(0) << endl;
  cout << commify(1) << endl;
  cout << commify(999) << endl;
  cout << commify(1000) << endl;
  cout << commify(1000000) << endl;
  cout << commify(1234567890ULL) << endl;
  return 0;
}

输出(ideone):

0
1
999
1,000
1,000,000
1,234,567,890

答案 4 :(得分:0)

// Accepts a long number, returns a comma formatted string
CString num_with_commas(long lnumber)
{
    CString num;
    num.Format(%d",lnumber);
    if(num.GetLength() > 3) {num.Insert(num.GetLength()-3, ',');}
    if(num.GetLength() > 7) { num.Insert(num.GetLength()-7, ','); }
    if (num.GetLength() > 12) { num.Insert(num.GetLength()-12, ','); }
    return(num);
}