开发者

Why is my number value changing 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". Note the l开发者_开发知识库ast six digits are completely different for no obvious reason (all i was asking it to do was insert thousand separators).


number_format(), as stated by the PHP manual, converts a float into a string with the thousands groups with commas.

$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're trying to cast a string to an integer, you can do so 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 cast a string like the one above to int on a 32-bit system, you'll assign $number to 2147483647 rather than the value you intend!

The number_format() function takes a float as an argument, which has similar issues. When you pass a string as an argument to number_format(), it is internally converted to a float. Floats are a little more complicated than integers. Instead of having a hard upper bound like an integer value, floats progressively lose precision in the least significant digits, making those last few places incorrect.

So, unfortunately, if you need to format long strings like this you'll probably need to write your own function. If you only need to add commas in the thousands places, this should be easy - just use strlen and substr to get every set of three characters from the end of string and create a new string with commas in between.


I know this is an old question, but I came across this issue recently and coded a function that works for my purpose. Hopefully it helps others. It works with single quote strings and $_GET. I haven't tested $_POST. Modifications will have to 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
?>


To work with PHP and such big numbers, You should use some library Working 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜