Does ostringstream inserter use current locale?

六眼飞鱼酱① 提交于 2019-12-11 10:25:26

问题


To convert double to string without scientific notation, the suggested way is

std::ostringstream strStream;
strStream << std::fixed;
strStream << value;
std::string str = strStream.str();

as in Prevent scientific notation in ostream when using << with double & How do I convert a double into a string in C++?

I want to convert double to string without any separator and Dot/Full-stop as the decimal point, i.e. ignoring locale.

From description of ostringstream, std::fixed and showpoint, I couldn't determine

  1. Whether the decimal point will be in current locale? and
  2. Whether there will be other separators (e.g. thousand groupings) in the string returned by str() based on locale?

And if its as per current locale, what is the way to override so that - its always Dot/Full-stop as decimal point and there are no grouping?


回答1:


Yes, the default locale for a stream is the current global locale at the time of construction (see description of basic_ios::init in your favorite reference and then check that the kind of stream does not do otherwise -- none of the standard classes does).

To change the locale, use basic_ios::imbue, thus you probably want to add

strStream.imbue(std::locale::classic());

after the definition of strStream.



来源:https://stackoverflow.com/questions/34925705/does-ostringstream-inserter-use-current-locale

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