改进的number_format()函数适用于大于2 ^ 53的数字

时间:2015-10-29 08:30:37

标签: php limit number-formatting

我为GET请求创建了一个输出数据的API。该数据也根据用户的语言环境进行格式化。我使用PHP number_format()函数,并注意到对于大于2 ^ 53的数字,数字格式输出不同的数字(它们近似它们)。这是一个问题所以我必须创建一个能够克服这个问题的函数。 请查看示例以了解问题:

$original_number = 9223372036854775805.123;
echo a_number_format($original_number, 4, ".", "'",3);
echo "<br />";
echo number_format($original_number, 4, ".", "'");
// Outputs:
9'223'372'036'854'775'805.1230
9'223'372'036'854'775'808.0000
// Please note that number_format() returns aproximate value for any number bigger than 2^53

1 个答案:

答案 0 :(得分:0)

function number_format($number_in_iso_format, $no_of_decimals=3, $decimals_separator='.', $thousands_separator='', $digits_grouping=3){
    // Check input variables
    if (!is_numeric($number_in_iso_format)){
        error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$number_in_iso_format is not a number.");
        return false;
    }
    if (!is_numeric($no_of_decimals)){
        error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$no_of_decimals is not a number.");
        return false;
    }
    if (!is_numeric($digits_grouping)){
        error_log("Warning! Wrong parameter type supplied in my_number_format() function. Parameter \$digits_grouping is not a number.");
        return false;
    }


    // Prepare variables
    $no_of_decimals = $no_of_decimals * 1;


    // Explode the string received after DOT sign (this is the ISO separator of decimals)
    $aux = explode(".", $number_in_iso_format);
    // Extract decimal and integer parts
    $integer_part = $aux[0];
    $decimal_part = isset($aux[1]) ? $aux[1] : '';


    // Extract the negative sign
    $sign='';
    if (strpos($integer_part,"-")===0){
        $sign = '-';
        $integer_part = substr($integer_part,1);
    }



    // Adjust decimal part (increase it, or minimize it)
    if ($no_of_decimals > 0){
        // Check actual size of decimal_part
        // If its length is smaller than number of decimals, add trailing zeros, otherwise round it
        if (strlen($decimal_part) < $no_of_decimals){
            $decimal_part = str_pad($decimal_part, $no_of_decimals, "0");
        } else {
            $decimal_part = substr($decimal_part, 0, $no_of_decimals);
        }
    } else {
        // Completely eliminate the decimals, if there $no_of_decimals is a negative number
        $decimals_separator = '';
        $decimal_part       = '';
    }

    // Format the integer part (digits grouping)
    if ($digits_grouping > 0){
        $aux = strrev($integer_part);
        $integer_part = '';
        for ($i=strlen($aux)-1; $i >= 0 ; $i--){
            if ( $i % $digits_grouping == 0 && $i != 0){
                $integer_part .= "{$aux[$i]}{$thousands_separator}";
            } else {
                $integer_part .= $aux[$i];          
            }
        }
    }

    $processed_number = "{$sign}{$integer_part}{$decimals_separator}{$decimal_part}";
    return $processed_number;
}