Why is changing a numeric value using number_format ()?

The following code gives me two different outputs:

$number = '1562798794365432135246'; echo $number; echo number_format($number); 

Can anyone explain this?

EDIT: forgot to mention, the above gives me "1562798794365432135246 1,562,798,794,365,432,233,984". Please note that the last six digits are completely different for some obvious reason (all I asked to do was insert thousands of delimiters).

+3
source share
3 answers

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.

+5
source

I know this is an old question, but I recently ran into this problem and encoded a function that works for my purpose. Hope this helps others. It works with single quotes and $ _GET. I have not tested $ _POST. Modifications must be made if you are dealing with negative integers or non-integers.

 <?php function format_big_numbers($number, $delimiter) { $len = strlen($number); if ($len > 3){ if ($len % 3 == 0) { $split = str_split($number, 3); $number_with_commas = implode("$delimiter", $split); return $number_with_commas; } else if ($len % 3 == 1) { $front = substr($number, 0, 1); $split = substr($number, 1, $len - 1); $split = str_split($split, 3); $number_with_commas = implode("$delimiter", $split); $number_with_commas = $front . "$delimiter" . $number_with_commas; return $number_with_commas; } else { $front = substr($number, 0, 2); $split = substr($number, 2, $len - 2); $split = str_split($split, 3); $number_with_commas = implode("$delimiter", $split); $number_with_commas = $front . "$delimiter" . $number_with_commas; return $number_with_commas; } } else { return $number; } } $num = '1234567891234567891234567891234'; echo format_big_numbers($num, ","); // output is 1,234,567,891,234,567,891,234,567,891,234 ?> 
+2
source

To work with PHP and such large numbers, you should use some library Work with large numbers in PHP

BC Math - http://www.php.net/manual/en/book.bc.php , GMP - http://www.php.net/manual/en/book.gmp.php

+1
source

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


All Articles