29

Like:

float(1.2345678901235E+19) => string(20) "12345678901234567890"

Can it be done?

(it's for json_decode...)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
tweety
  • 293
  • 1
  • 3
  • 4

6 Answers6

55
echo number_format($float,0,'.','');

note: this is for integers, increase 0 for extra fractional digits

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 2
    This is a good solution if you want to have a maximum number of decimal places. Based on the question, I'd change the above to `echo number_format($float,10,'.','');` giving it a max of 10 decimal places. (Arbitrary, but I'm pretty sure it should be higher than 0). – Anthony Jul 29 '11 at 17:26
  • The json value I get is not float, it's a really big integer, like 23453453245324532453253425. But it gets converted to float by json_decode. Will your solution always get me the original json value? :) (it seems to work though for the data I have now) – tweety Jul 29 '11 at 17:27
  • floats have a limited precision, once your integers are large enough it won't work. should work for approx 14 digits. beyond that it might work, but could be sheer luck. – Karoly Horvath Jul 29 '11 at 17:29
  • The value has 17 numbers right now. Do you know when it will stop to precisely convert the float? – tweety Jul 29 '11 at 17:32
  • You can change the setting in the function call, see my answer. – Anthony Jul 29 '11 at 17:33
  • are these originally floats? because then they already lost precision. if they are ints try to send them as strings. – Karoly Horvath Jul 29 '11 at 17:34
  • [`number_format` docs](https://www.php.net/manual/en/function.number-format.php) – Tomás Cot Jul 04 '22 at 17:19
18
$float = 0.123;
$string = sprintf("%.3f", $float); // $string = "0.123";
lubart
  • 1,746
  • 3
  • 27
  • 35
2

It turns out json_decode by default casts large integers as floats. This option can be overwritten in the function call:

$json_array = json_decode($json_string, , , 1);

I'm basing this only on the main documentation, so please test and let me know if it works.

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • that's the first thing I tried - JSON_BIGINT_AS_STRING, and I get: json_decode expects at most 3 parameters bla bla... and a undefined constant message – tweety Jul 29 '11 at 17:34
  • 1
    Ha! I just tested and I got the same error, but with "expects at most 2 parameters". It looks like the 3rd parameter was added in 5.3 and the 4th in 5.4. So it would work if we upgraded our php. – Anthony Jul 29 '11 at 17:53
  • Anyway, it's not even valid code, you can't omit arguments like that. – netcoder Jul 29 '11 at 17:58
  • can't you? It says I passed in 4 parameters... Let me do a test. – Anthony Jul 29 '11 at 23:01
0

I solved this issue by passing the argument JSON_BIGINT_AS_STRING for the options parameter.

json_decode($json, false, 512, JSON_BIGINT_AS_STRING)

See example #5 in the json_decode documentation

Astrotim
  • 2,152
  • 19
  • 23
0

The only way to decode a float without losing precision is to go through the json and frame all the floats in quotation marks. By making strings of numbers.

PHP json_decode integers and floats to string

0

A double precision floating point number can only contain around 15 significant digits. The best you could do is pad the extra digits out with zeroes.

Alnitak
  • 334,560
  • 70
  • 407
  • 495