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:
{"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.
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)