Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing big 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
like image 633
Omid Avatar asked Nov 23 '22 23:11

Omid


1 Answers

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

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

Note that PHP is using a signed integer internally. The size depends on your system.

32bit system:

2^(32-1) = 2147483648

64bit system:

2^(64-1) = 9223372036854775808

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

like image 135
hek2mgl Avatar answered Nov 26 '22 23:11

hek2mgl