|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-08-12 00:31 UTC] jmuraca at gmail dot com
Description:
------------
json_encode should be able to accept the parameter JSON_BIGINT_AS_STRING to display large numbers as string and/or numeric rather than scientific notation
my use case is a long number like that is used for some account management identifiers (my included example is fake) and I want to json output the whole number - not a scientific number
Test script:
---------------
// correct, displays scientific notation
$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data));
// correct, displays val as string
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data));
// correct, displays val as numeric
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK));
// ERROR, displays val as scientific
// ideally with JSON_BIGINT_AS_STRING val display as numeric and full number
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK|JSON_BIGINT_AS_STRING));
// ERROR, displays val as scientific
// ideally with JSON_BIGINT_AS_STRING val display as numeric and full number
$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data, JSON_BIGINT_AS_STRING));
Expected result:
----------------
$data = array("val" => "1008901020901566350119");
var_dump(json_encode($data, JSON_NUMERIC_CHECK|JSON_BIGINT_AS_STRING));
// actual: 1.0089010209015663e+21
// expected: 1008901020901566350119
$data = array("val" => 1008901020901566350119);
var_dump(json_encode($data, JSON_BIGINT_AS_STRING));
// actual: 1.0089010209015663e+21
// expected: 1008901020901566350119
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 11:00:02 2025 UTC |
If I load a long number from a database, which in this case happens to be a valid ID number, I'd like to preserve that formatting and not convert to scientific notation when encoding to JSON. I understand the MAX INT value for a system so I can get around this by treating it as a string, but when I call json_encode with the JSON_NUMERIC_CHECK flag set, it converts to scientific notation. $data = array("val_num" => 1008901020901566350119, "val_string" => "1008901020901566350119", "int" => "1", "string" => "12345"); var_dump(json_encode($data, JSON_NUMERIC_CHECK)); // returns: {"val_num":1.0089010209015663e+21,"val_string":1.0089010209015663e+21,"int":1,"string":12345} I'd like to be able to handle small int as numbers, and big int as string or numbers. JSON_BIGINT_AS_STRING feel like the correct parameter to output this - literally any big integers are encoded to a string.If you load a big integer (one that exceeds PHP_INT_MAX) from a database and you try to use it in PHP as a number, then the conversion to double happens when you store it in PHP. In your example $data = array("val_num" => 1008901020901566350119, "val_string" => "1008901020901566350119", "int" => "1", "string" => "12345"); $data['val_num'] is already a floating-point number (try var_dump($data) to see); the conversion has nothing to do with JSON, and the encoder is only given a floating-point PHP number (and not a "bigint"). (Since the number is an identifier and not something that actually involves any arithmetic I could argue that it's not an integer in the first place, just a string written with a particularly limited alphabet. But that's a database design issue for somewhere else.)