问题
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
- Whether the decimal point will be in current locale? and
- 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