33

Like:

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

Can it be done?

(it's for json_decode...)

8 Answers 8

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

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

Sign up to request clarification or add additional context in comments.

7 Comments

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).
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)
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.
The value has 17 numbers right now. Do you know when it will stop to precisely convert the float?
You can change the setting in the function call, see my answer.
|
22
$float = 0.123;
$string = sprintf("%.3f", $float); // $string = "0.123";

Comments

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.

4 Comments

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
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.
Anyway, it's not even valid code, you can't omit arguments like that.
can't you? It says I passed in 4 parameters... Let me do a test.
1

One funny way is to use json_encode()

json_encode($float, JSON_PRESERVE_ZERO_FRACTION)

Comments

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

Comments

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

Comments

0

I often use:

$v = float(1.2345678901235E+19);
$s = "{$v}";

Not sure if there may be any "gotcha's" associated but it generally works for me.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.