Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - 8 decimals don't work

Tags:

php

Here is a very simple code.

$a = 2000000.00000000;
$b = 0.00000001;

echo $a-$b; //output 2000000

I was expecting 1999999.99999999. 

Can someone explain how to make it work ? It would be very appreciate.

like image 521
BlackCrypto Avatar asked Dec 06 '25 03:12

BlackCrypto


1 Answers

You can set the precision used when formatting floats as strings using the precision ini setting, the default being 14:

ini_set('precision', 16);
echo $a - $b; // 1999999.99999999

Also, read this article for a more generic breakdown on the subject.

like image 186
3 revs, 2 users 93%Ja͢ck Avatar answered Dec 12 '25 13:12

3 revs, 2 users 93%Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!