Print integer with thousands and millions separator

后端 未结 2 1917
一整个雨季
一整个雨季 2020-12-11 01:41

Got a question about printing Integers with thousands/millions separator.

I got a Textfile where i got Country, City,Total Population.

I have to read in the

2条回答
  •  抹茶落季
    2020-12-11 02:19

    imbue() the output stream with a locale that has the desired separator. For example:

    #include 
    #include 
    
    int main()
    {
        // imbue the output stream with a locale.
        int i = 45749785;
        std::cout << i << "\n";
    
        std::cout.imbue(std::locale(""));
        std::cout << i << "\n";
    }
    

    Output on my machine (and online demo):

    45749785
    45,749,785
    

    As commented, and answered, by James Kanze imbue the input stream also to read the separated int values without manually modifying the input.


    See Stroustrop's Appendix D: Locales for a detailed overview of locales.

提交回复
热议问题