在c ++中从整数插入和删除逗号 [英] Inserting and removing commas from integers in c++

查看:119
本文介绍了在c ++中从整数插入和删除逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在写一个小应用程序,它工作得很好,但是可读性是我的数字的噩梦。



基本上,我想做的是在屏幕上显示的数字中添加逗号,以便于阅读。有没有一个快速和容易的方法来做到这一点?



我一直在使用stringstream抓住我的数字(我不知道为什么这甚至建议在这一点,它只是建议在教程中我通过),如(裁剪出无关的位):

  #include< ; iostream> 
#include< string>
#include< sstream>
using namespace std;

int items;
string stringcheck;

...

cout< 输入您有多少项:;
getline(cin,stringcheck);
stringstream(stringcheck)>>项目;

...

cout< \\\
You have<<项目< items.\\\
;

当这个数字被输入为大的时候,在其他一切之间变得很头痛。 / p>

有没有任何快速和简单的方法使它打印13,653,456,而不是像现在的13653456(假设是当然的输入)?



注意:如果重要的话,我将其作为Microsoft Visual C ++ 2008 Express Edition中的控制台应用程序。

解决方案

尝试 数字 分面,超载 do_thousands_sep 函数。有一个示例。我也黑了一些只是解决你的问题:

  #include< locale> 
#include< iostream>

class my_numpunct:public std :: numpunct< char> {
std :: string do_grouping()const {return\3; }
};

int main(){
std :: locale nl(std :: locale(),new my_numpunct);
std :: cout.imbue(nl);
std :: cout<< 1000000 < \\\
; //不使用千位分隔符
std :: cout.imbue(std :: locale());
std :: cout<< 1000000 < \\\
; // uses thousands'separators
}


Very much a noob here, so it's best to assume I know nothing in any answers.

I've been writing a little app, and it's working well, but readability is a nightmare with my numbers.

Essentially, all I want to do is to add commas into the numbers displayed on the screen to make it easier to read. Is there a quick and easy way to do this?

I've been using stringstream to grab my numbers (I'm not sure why this is even suggested at this point, it was simply advised in the tutorial I worked through), such as(cropping out the irrelevent bits):

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int items;
string stringcheck;

...

    cout << "Enter how many items you have: ";
        getline (cin, stringcheck);
        stringstream(stringcheck) >> items;

...

    cout << "\nYou have " << items << " items.\n";

When that number is typed as something large, amongst everything else it becomes quite a headache to read.

Is there any quick and easy way to make it print "13,653,456" as opposed to "13653456" like it would right now (assuming that's what was input of course)?

Note: If it matters, I'm making this as a console application in Microsoft Visual C++ 2008 Express Edition.

解决方案

Try the numpunct facet and overload the do_thousands_sep function. There is an example. I also hacked up something that just solves your problem:

#include <locale>
#include <iostream>

class my_numpunct: public std::numpunct<char> {
    std::string do_grouping() const { return "\3"; }
}; 

int main() {
    std::locale nl(std::locale(), new my_numpunct); 
    std::cout.imbue(nl);
    std::cout << 1000000 << "\n"; // does not use thousands' separators
    std::cout.imbue(std::locale());
    std::cout << 1000000 << "\n"; // uses thousands' separators
} 

这篇关于在c ++中从整数插入和删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆