防止像这样格式化大数字"24.000.000"

Prevent a big number from being formated like this "24.000.000"

本文关键字:数字 像这样 格式化      更新时间:2023-10-16

我在我的ostream中插入了一些int s,但大数字或甚至年份,如300025000123正在像这样格式化3.00025.000.123

我不确定为什么会发生这种情况。这可能是因为我在流上使用了imbue(""),所以十进制数字像这样显示14,53而不是14.53,但是我注释了那行,所以一切都保持原样。

我只是想在输出中获得数字时去掉那些点(但我也不想去掉小数点逗号)。我该怎么做呢?

我想也许iomanip库会有所帮助,但我没有找到任何关于这种情况的东西。

std::ostream& operator <<(std::ostream& os, const Article& a) {
    os << a.title() << a.pub_date().year() << ". " << a.price() << "";
    return os;
}

如果您为数字指定了自定义分组,则可以继续使用imbue

请看下面的例子:http://www.cplusplus.com/reference/locale/numpunct/grouping/

在这个例子中,我们有这段代码,它验证即使设置了区域设置也不进行分组。

// numpunct::grouping example
#include <iostream>       // std::cout
#include <string>         // std::string
#include <locale>         // std::locale, std::numpunct, std::use_facet
// custom numpunct with grouping:
struct my_numpunct : std::numpunct<char> {
    // the zero by itself means do no grouping
    std::string do_grouping() const {return "";}
};
int main() {
    std::locale loc (std::cout.getloc(),new my_numpunct);
    std::cout.imbue(loc);
    std::cout << "one million: " << 1000000 << 'n';
    return 0;
}