简体   繁体   中英

Subtract 1 from a large number in PHP

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

$a = 294867828828426241 - 1;

I receive the floating point number 2.94867828828E+17 . Which, when resolved by number_format() gives the original number.

How can i get the correct value, please?

This needs to be able to be able work with different numbers.

If you have the BCMath extension you can use this:

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

echo $a; // 294867828828426240

However, testing on my 64-bit server your code should work correctly. I'm not sure, but you can check to see if increasing the precision directive in your php.ini will make any difference. I have mine set at 14.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM