|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-11-19 22:18 UTC] admin at bittylicious dot com
Description:
------------
There should be an option that can be passed to json_decode that will allow numbers to be handled as strings. This is because some numbers can not be represented in a float properly. In reality, JSON is often used as a transport layer, and it's very useful to keep this accuracy.
One example in practice where this has caused issues is when dealing with Dogecoin (a Bitcoin derivative). Dogecoin has eight decimal places, and because each Dogecoin is worth only a tiny fraction of a cent, they can often be represented as tens or hundreds of millions of Dogecoins. When you're dealing with finance, it's essential not to lose precision. The only workaround I have is to use a complicated preg_replace to add quotes to the original JSON string, which is nasty.
Test script:
---------------
<?php
print_r(json_decode('{"test":1234567890.12345678}'));
?>
Output:
stdClass Object
(
[test] => 1234567890.1235
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 09:00:01 2025 UTC |
I do understand what you're saying, but my feature request is under the assumption you never ever need to convert this to a float, and my real life use case is indeed this situation. Simplifying things, let's imagine a JSON service that simply multiplies a number by two. You would pass in: {"val":1234567890.12345678} But you would have no way to even pass this to bcmul (which I believe works on strings) without losing precision even if bcmul needn't have any precision issues. Consider another service. This simply forwards "val" onwards perhaps with a hash signature or something. Consider this a "signing service". It cannot read this 'val' value without losing precision, when I think it should be possible to interpret this as a string. This is actually more similar to my use case, in other words, using JSON really as a transport mechanism and not manipulating these numbers within the PHP script.