Subtract 1 from a large number in PHP

I need to subtract 1 from a number 294867828828426241in PHP. However when i do

$a = 294867828828426241 - 1;

I get a floating point number 2.94867828828E+17. Which at resolution number_format()gives the original number.

How can I get the correct value, please?

This should be able to work with different numbers.

+5
source share
1 answer

If you have a BCMath extension , you can use this:

$a = bcsub('294867828828426241', '1');

echo $a; // 294867828828426240

However, when testing on my 64-bit server, your code should work correctly. I'm not sure, but you can check to see if the increase in precision in yours changes php.ini. I have my number set to 14.

+7
source

All Articles