number_format() , as stated in the PHP manual , converts a float to a string with thousands of comma-separated groups.
$number = '1562798794365432135246'; var_dump($number); // string(22) "1562798794365432135246" var_dump(number_format($number)); // string(29) "1,562,798,794,365,432,233,984"
If you are trying to pass a string to an integer, you can do it like this:
$number = (int) $number;
However, be careful, since the largest possible integer value in PHP is 2147483647 on a 32-bit system and 9223372036854775807 on a 64-bit system. If you try to overlay a string like the one above on an int on a 32-bit system, you will assign $number to 2147483647 , not the value you intend to!
The number_format() function takes a float as an argument, which has similar problems. When you pass a string as an argument to number_format() , it is internally converted to float. Floats are a bit more complicated than whole ones. Instead of having a hard upper bound, such as an integer value, the floats gradually lose accuracy in the least significant digits, which makes these last few places wrong.
So, unfortunately, if you need to format long lines like this, you probably have to write your own function. If you only need to add commas to thousands of places, this should be easy - just use strlen and substr to get each set of three characters from the end of the line and create a new line with commas between them.
source share