mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-06-07 09:04:57 +02:00
Use std::stringstream instead of snprintf() for double->string conversion
`snprintf()` will use the current `LC_NUMERIC` locale for converting a double to a string, which will use a `,` instead of a `.` in some locales (e.g. de_DE). `std::stringstream` allows setting the locale to `"C"` to always get a `.`. This occurs only for that `stringstream` instance; no global is altered.
This commit is contained in:
parent
06dcb1fc89
commit
b8aaa03367
@ -63,22 +63,15 @@ std::string valueToString(UInt value) {
|
|||||||
#endif // # if defined(JSON_HAS_INT64)
|
#endif // # if defined(JSON_HAS_INT64)
|
||||||
|
|
||||||
std::string valueToString(double value) {
|
std::string valueToString(double value) {
|
||||||
// Allocate a buffer that is more than large enough to store the 16 digits of
|
// We need not request the alternative representation
|
||||||
// precision requested below.
|
// that always has a decimal point because JSON doesn't distingish the
|
||||||
char buffer[32];
|
// concepts of reals and integers.
|
||||||
|
std::stringstream str;
|
||||||
// Print into the buffer. We need not request the alternative representation
|
// Set locale to "C" to always get a '.' instead of a ','
|
||||||
// that always has a decimal point because JSON doesn't distingish the
|
str.imbue(std::locale::classic());
|
||||||
// concepts of reals and integers.
|
str.precision(16);
|
||||||
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with
|
str << value;
|
||||||
// visual studio 2005 to
|
return str.str();
|
||||||
// avoid warning.
|
|
||||||
sprintf_s(buffer, sizeof(buffer), "%.16g", value);
|
|
||||||
#else
|
|
||||||
snprintf(buffer, sizeof(buffer), "%.16g", value);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string valueToString(bool value) { return value ? "true" : "false"; }
|
std::string valueToString(bool value) { return value ? "true" : "false"; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user