Menu

#61 Decimal separator uses locale default character ( 0.6.0rc2)

0.5.0
closed-duplicate
nobody
decimal (1)
5
2015-03-02
2013-07-03
drkzs
No

Steps
1.write a simple programm in c++ using jsoncpp that does :
a) create a Value of type double ("myDouble",3.5)
b) print Value using FastWriter or StyledWriter (or Value.toStyledString)
2.set locale to fr_FR.UTF-8 in a terminal (i guess all locales that use by default the "," character as decimal separator
3.execute program from the terminal

Result
I expect to see {"myDouble":3.5}
but I see {"myDouble":3,500000000000000}

Which version
OS: Fedora 18
jsoncpp: 0.6.0-0.9.rc2

Remarks
With a local as en_US, there is no problem.

Maybe there is an option that i didn't see, if it's so, sorry

Maybe it's not a bug, i guess it can appear logical that the library uses the default decimal separator of the local... But when a server expects a specific format for double, whatever the local is, the problem can be a real issue, and the workaround of changing locale is not really a good idea :/ Just for information, rapidjson behaves quite the same, boost doesn't.

Discussion

  • ET

    ET - 2013-07-18

    It's clear that this issue wasn't considered, because the code of valueToString(double) assumes that the decimal point is a point, and truncates zeroes based on that, so this truncation doesn't happen when it's a comma.

    Using the locale like this is inherent in printf which is used for the conversion (sprintf or sprintf_s for Windows). It would probably be best to use _sprintf_s_l instead of _sprintf_s and use a locale with a point, or to make this configurable. sprintf_l should work similarly on other platforms (using xlocale.h).

    Here's the code I used to fix this:

    _locale_t locale = _create_locale(LC_NUMERIC, "English");
    _sprintf_s_l(buffer, sizeof(buffer), "%#.16g", locale, value);
    _free_locale(locale);

     

    Last edit: ET 2013-07-18
  • aqpage

    aqpage - 2014-04-09

    This is a duplicate of [#43]

     

    Related

    Bugs: #43

  • Christopher Dunn

    • status: open --> closed-duplicate
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.