PHP function json_decode decodes float value with zeros after point as int

I have a JSON string containing some key with the following value: 123.00. When I use the json_decode function, I get a decoded string where the previous key is 123, not 123.00. Is there a way to fix the decoding of such values โ€‹โ€‹without using quotes?

+4
source share
5 answers

It is currently being raised as a PHP error:

Error Report : https://bugs.php.net/bug.php?id=50224

In the future, there may be functionality for passing a flag through the options parameter for stronger typing. At this point, however, wrapping it in quotation marks will suffice.

+7
source

I do not think that's possible!

+1
source
 //convert the json to a string before json_decode $res = preg_replace( '/next_cursor":(\d+)/', 'next_cursor":"\1"', $json ); 
+1
source
 number_format($number, 2) 

print a number through this?

0
source

You can use the JSON_BIGINT_AS_STRING option, for example:

 $json = json_decode($input, true, 512, JSON_BIGINT_AS_STRING); 

Caution, but this only works with PHP 5.4+!

0
source

Source: https://habr.com/ru/post/1435505/


All Articles