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

I have a JSON string that contains some key with the following value: 123.00. When I use json_decode function I get the decoded string where the previous key equal to 123, not to 123.00. Is there a way to correct decode such values without wrapping into quotes?

Answers


This is currently being brought up as a PHP bug:

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

In the future, there may be functionality to pass a flag through the options parameter for stricter typing. For now, however, wrapping it in quotes will have to suffice.


I do not think it is possible!


number_format($number, 2) 

output the number through that?


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

You can use the JSON_BIGINT_AS_STRING option, for example:

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

Careful though, this only works with PHP 5.4+!


Need Your Help

webgl three.js get mouse position on Mesh

javascript cursor position three.js webgl

Honestly, i tried to find solution for question below, please help me. And sorry for my English :)

Converting money formatted strings to floats

php regex floating-point numbers

I have to read a rather large file, containing numbers, formatted in different ways.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.