Printing large numbers in PHP

The number is 13911392101301011 , and regardless of using sprintf or number_format , I get the same strange result.

 sprintf('%017.0f', "13911392101301011"); // Result is 13911392101301012 number_format(13911392101301011, 0, '', ''); // Result is 13911392101301012 sprintf('%017.0f', "13911392101301013"); // Result is 13911392101301012 number_format(13911392101301013, 0, '', ''); // Result is 13911392101301012 
+4
source share
6 answers

This is a job. > !!

 $num1 = "13911392101301011"; $r = mysql_query("Select @sum:=$num1"); $sumR = mysql_fetch_row($r); echo $sum = $sumR[0]; 
0
source

You can do it as follows: -

 ini_set("precision",25); // change 25 to whatever number you want or need $num = 13911392101301011; print $num; 
0
source

Since you are dealing with large numbers here, you can save them as strings and perform a numerical operation on string values ​​using BCMath functions .

 $val = "13911392101301011"; echo $val; // 13911392101301011 echo bcadd($val, '4'); // 13911392101301015 echo bcmul($val, '2'); // 27822784202602022 
0
source

Since you actually have a number as a string, use the %s modifier:

 sprintf('%s', "13911392101301011"); // 13911392101301011 

Note that PHP uses a signed integer. The size depends on your system.

32-bit system:

 2^(32-1) = 2147483648 

64-bit system:

 2^(64-1) = 9223372036854775808 

-1 because 1 bit is reserved for the sign flag.

0
source

The documentation states that $ number in number_format is a float , so there is an explicit type. The equivalent will look like this:

 sprintf('%017.0f', (float) "13911392101301011"); 

The float is accurate to 14 digits and your number has 17 digits.

0
source

Your call to number_format sets the value. and to space

 string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' ) 

try the following:

 number_format(13911392101301011, 0, '.', ','); 
-1
source

Source: https://habr.com/ru/post/1479862/


All Articles