6

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
Omid
  • 4,575
  • 9
  • 43
  • 74
  • Random idea: Is the number being interpreted as a `float` or `double` and rounded to the nearest valid value of such a data type? Noticed that it's too large to fit in a 32 bit integer, it could fit in a 64 bit integer though but not sure if it is. – Patashu May 09 '13 at 06:05
  • I know that's too large, But i found using `sprintf` or `number_format` should solve the problem. Unfortunately not working in this case – Omid May 09 '13 at 06:10
  • Interesting that `13911392101301014` is being printed correctly and `13911392101301011` is not, so its not a range issue – Hanky Panky May 09 '13 at 06:13
  • @ØHankyPankyØ Yes, That's the most strange thing to me too. When you try to echo `13911392101301015` the problem exists again and result is `13911392101301016` – Omid May 09 '13 at 06:16

5 Answers5

1

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.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

You can do easily this way :-

ini_set("precision",25); // change 25 to whatever number you want or need 
$num = 13911392101301011;
print $num;
Antony
  • 14,900
  • 10
  • 46
  • 74
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
0

Since you are dealing with large numbers here, you may want to keep them as strings and perform numerical operation on the string values using BCMath functions.

$val = "13911392101301011";

echo $val; // 13911392101301011
echo bcadd($val, '4'); // 13911392101301015
echo bcmul($val, '2'); // 27822784202602022
Antony
  • 14,900
  • 10
  • 46
  • 74
0

Documentation states that $number in number_format is float so there is explicit typecast. Equivalent would look like this:

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

Float is precise to around 14 digits and your number has 17 digits.

mishak
  • 33
  • 6
-1

Your number_format call is setting the . and , to blank

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

try this:

number_format(13911392101301011, 0, '.', ',');
Revent
  • 2,091
  • 2
  • 18
  • 33
  • The result is `13,911,392,101,301,012` – Omid May 09 '13 at 06:08
  • Isn't that what you're trying to do? Oh, your issue is the "rounding" of the last digit either up or down. What version of PHP are you using? – Revent May 09 '13 at 06:09
  • Wrong answer. The OP is not trying to format the number, but to print its value as it is stated. – Antony May 09 '13 at 06:12
  • No friend. When i echo the number directly, The result is `1.3911392101301E+16`. Using `number_format` was a solution to solve this problem. – Omid May 09 '13 at 06:12
  • Check out some of the answers and comments here: http://stackoverflow.com/questions/6936062/why-is-my-number-value-changing-using-number-format – Revent May 09 '13 at 06:15