How to print a double with a comma

后端 未结 6 1321
醉话见心
醉话见心 2020-12-15 10:07

In C++ I\'ve got a float/double variable.

When I print this with for example cout the resulting string is period-delimited.

cout << 3.1415 <         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 10:19

    This is controlled by your program's locale.

    How you set a program's default locale depends on the platform. On POSIX type platforms, it's with the LANG and LC_* environment variables, for instance.

    You can force a particular locale -- different from the default -- within a C++ program by calling ios::imbue. Something like this might work:

    #include 
    cout.imbue(std::locale("German_germany"));
    

    The idea is to force a locale where comma is the decimal separator. You might need to adjust the "German_germany" string to get the behavior you want on your particular platform.

提交回复
热议问题