C++:用逗号格式化数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7276826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++: Format number with commas?
提问by User
I want to write a method that will take an integer and return a std::string
of that integer formatted with commas.
我想编写一个方法,它接受一个整数并返回一个std::string
用逗号格式化的整数。
Example declaration:
示例声明:
std::string FormatWithCommas(long value);
Example usage:
用法示例:
std::string result = FormatWithCommas(7800);
std::string result2 = FormatWithCommas(5100100);
std::string result3 = FormatWithCommas(201234567890);
// result = "7,800"
// result2 = "5,100,100"
// result3 = "201,234,567,890"
What is the C++ way of formatting a number as a string
with commas?
string
用逗号将数字格式化为 a 的 C++ 方式是什么?
(Bonus would be to handle double
s as well.)
(奖励也是处理double
s 。)
采纳答案by Jacob
Use std::locale
with std::stringstream
使用std::locale
与std::stringstream
#include <iomanip>
#include <locale>
template<class T>
std::string FormatWithCommas(T value)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss << std::fixed << value;
return ss.str();
}
Disclaimer:Portability might be an issue and you should probably look at which locale is used when ""
is passed
免责声明:可移植性可能是一个问题,您可能应该查看""
传递时使用的语言环境
回答by Node
You can do as Jacob suggested, and imbue
with the ""
locale - but this will use the system default, which does not guarantee that you get the comma. If you want to force the comma (regardless of the systems default locale settings) you can do so by providing your own numpunct
facet. For example:
您可以按照 Jacob 的建议进行操作,并imbue
使用""
语言环境 - 但这将使用系统默认值,这并不能保证您得到逗号。如果您想强制使用逗号(无论系统默认区域设置如何),您可以通过提供您自己的numpunct
方面来实现。例如:
#include <locale>
#include <iostream>
#include <iomanip>
class comma_numpunct : public std::numpunct<char>
{
protected:
virtual char do_thousands_sep() const
{
return ',';
}
virtual std::string do_grouping() const
{
return "";
}
};
int main()
{
// this creates a new locale based on the current application default
// (which is either the one given on startup, but can be overriden with
// std::locale::global) - then extends it with an extra facet that
// controls numeric output.
std::locale comma_locale(std::locale(), new comma_numpunct());
// tell cout to use our new locale.
std::cout.imbue(comma_locale);
std::cout << std::setprecision(2) << std::fixed << 1000000.1234;
}
回答by carljalal
I consider the following answer to be easier than the others:
我认为以下答案比其他答案更容易:
string numWithCommas = to_string(value);
int insertPosition = numWithCommas.length() - 3;
while (insertPosition > 0) {
numWithCommas.insert(insertPosition, ",");
insertPosition-=3;
}
This will quickly and correctly insert commas into your string of digits.
这将快速正确地将逗号插入到您的数字字符串中。
回答by troyane
If you are using Qt, you can use this code:
如果您使用的是 Qt,则可以使用以下代码:
const QLocale & cLocale = QLocale::c();
QString resultString = cLocale.toString(number);
Also, do not forget to add #include <QLocale>
.
另外,不要忘记添加#include <QLocale>
.
回答by Tom Serink
This is pretty old school, I use it in large loops to avoid instantiating another string buffer.
这是非常古老的学校,我在大循环中使用它以避免实例化另一个字符串缓冲区。
void tocout(long a)
{
long c = 1;
if(a<0) {a*=-1;cout<<"-";}
while((c*=1000)<a);
while(c>1)
{
int t = (a%c)/(c/1000);
cout << (((c>a)||(t>99))?"":((t>9)?"0":"00")) << t;
cout << (((c/=1000)==1)?"":",");
}
}
回答by Radif Sharafullin
based on the answers above, I ended up with this code:
根据上面的答案,我最终得到了这个代码:
#include <iomanip>
#include <locale>
template<class T>
std::string numberFormatWithCommas(T value){
struct Numpunct: public std::numpunct<char>{
protected:
virtual char do_thousands_sep() const{return ',';}
virtual std::string do_grouping() const{return "";}
};
std::stringstream ss;
ss.imbue({std::locale(), new Numpunct});
ss << std::setprecision(2) << std::fixed << value;
return ss.str();
}
回答by Marcel
To make it more flexible, you could construct the facet with a custom thousands sep and grouping string. This way you can set it at runtime.
为了使其更加灵活,您可以使用自定义的数千个 sep 和分组字符串来构建构面。这样你就可以在运行时设置它。
#include <locale>
#include <iostream>
#include <iomanip>
#include <string>
class comma_numpunct : public std::numpunct<char>
{
public:
comma_numpunct(char thousands_sep, const char* grouping)
:m_thousands_sep(thousands_sep),
m_grouping(grouping){}
protected:
char do_thousands_sep() const{return m_thousands_sep;}
std::string do_grouping() const {return m_grouping;}
private:
char m_thousands_sep;
std::string m_grouping;
};
int main()
{
std::locale comma_locale(std::locale(), new comma_numpunct(',', ""));
std::cout.imbue(comma_locale);
std::cout << std::setprecision(2) << std::fixed << 1000000.1234;
}