json_decode reformatting decimals in JSON based on locale

I’m currently working on a site that’s localized in a couple of languages and I’m running into a problem where json_decode is reformatting decimals in JSON strings depending on the locale. When the locale is set to “en” the decimals remain untouched. However, in the “fr_FR” locale they get changed to “13,3” for example.

Source JSON:

Read More
{"debug":[{"id":13.3}]}

Output for “en”

Array
(
    [debug] => Array
        (
            [0] => Array
                (
                    [id] => 13.3
                )
        )
    )

Output for “fr_FR”

Array
(
    [debug] => Array
        (
            [0] => Array
                (
                    [id] => 13,3
                )
        )
    )

Is there any reason json_decode does this? Is there a way to prevent it?

The bug is causing problems with the Gravity Forms WordPress plugin, but I’ve already isolated the problem to the json_decode function.

Related posts

Leave a Reply

1 comment

  1. It looks like the issue is with how PHP handles numeric values. json_decode is simply transforming 13.3 to a float, which on output or being converted to a string is the localized “13,3”. However, PHP does not deal well with localized number formats.

    As per this thread, using setlocale has resolved the issue (for the most part)

    setlocale(LC_NUMERIC, 'C');