Why is my number value changing using number_format()?

前端 未结 3 1033
悲&欢浪女
悲&欢浪女 2020-12-01 23:04

The following code gives me two different outputs:

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

Can an

3条回答
  •  没有蜡笔的小新
    2020-12-01 23:43

    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.

     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
    ?>
    

提交回复
热议问题