Reputation: 271
New to programming and taking a PHP course. I wrote a very small program to test out the number_format function. Here is my code:
$num = 9876543210123456789;
$result = number_format($num);
echo = $result;
The output is: 9,876,543,210,123,456,512
I'm sure it has something to do with the length of $num. But haven't been able to find the exact explanation/reason why this happens.
Upvotes: 0
Views: 1486
Reputation: 8950
You can't pass large numbers to the number_format()
function. If you just need to format the number, you can write your own function to separate the numbers with a comma taking 3 by 3.
Reason is that if you check the manual, the number format takes the first parameter as a float. You need to have a good understanding about float
, and ints
to understand why it happens.
Here is something interesting I found in SO, it is same as your question, and it has a good answer. Please check it.
Why is my number value changing using number_format()?
Upvotes: 1
Reputation: 46900
You're seeig a different number because the number you have provided is large enough to cause an overflow on your system. Try with a smaller number
<?php
$num = 9876543210123;
$result = number_format($num);
echo $result; // will show as expected, with formatting
Even if you don't use number_format
on that number, your number will still cause overflow. Try this
$num = 9876543210123456789;
echo $num; // not what you see above
Also see:
What's the maximum size for an int in PHP?
Upvotes: 2