Output numbers with digit grouping (1000000 as 1,000,000 and so on)

。_饼干妹妹 提交于 2019-11-30 18:49:22

According to this thread, you can set a locale on your output stream by doing something like:

#include <iostream>
#include <locale>
#include <string>

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() {
    std::locale global;
    std::locale withgroupings(global, new my_facet);
    std::locale was = std::cout.imbue(withgroupings);
    std::cout << 1000000 << std::endl;
    std::cout.imbue(was);

    return 0;
}

Haven't tried it myself but it certainly sounds like a reasonable approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!