0

I agree with this

php > var_dump(number_format(10000000000000000000000)); // 10^22
php shell code:1:
string(30) "10,000,000,000,000,000,000,000"

What's happening here?

php > var_dump(number_format(100000000000000000000000)); // 10^23
php shell code:1:
string(30) "99,999,999,999,999,991,611,392"

Can someone explain to me what exactly is going on here?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Friedrich Roell
  • 437
  • 1
  • 5
  • 14
  • 2
    Possible duplicate of [Why is my number value changing using number\_format()?](https://stackoverflow.com/questions/6936062/why-is-my-number-value-changing-using-number-format) – gvgvgvijayan Apr 25 '18 at 14:14

3 Answers3

3

This is well-described in the documentation:

Integer overflow

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

Example #3 Integer overflow on a 64-bit system

<?php
$large_number = 9223372036854775807;
var_dump($large_number);                     // int(9223372036854775807)

$large_number = 9223372036854775808;
var_dump($large_number);                     // float(9.2233720368548E+18)

$million = 1000000;
$large_number =  50000000000000 * $million;
var_dump($large_number);                     // float(5.0E+19)
?>

There are many values that floating-point numbers can't exactly represent. See Is floating point math broken? for details.

The PHP_INT_MAX constant shows the largest integer your version of PHP supports.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
0

Floating point numbers.

Computer memory is limited, so storing an infinitely precise integer value is impossible. Your system basically ran out of room for precise integer valuation.

This answer does a great job talking about how to get the integer to appear at the number you want it though.

logos_164
  • 736
  • 1
  • 13
  • 31
0

Change the precisiona in php.ini or use ini_set and apply cast in your var

ini_set('precision', 2048);
$number1 = (float) "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001";
$number3 = (float) "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001";

$test1 = (0 == $number1);
$test2 = (0 == $number3);