Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 改进的number_format()函数,用于处理大于2^53的数字_Php_Limit_Number Formatting - Fatal编程技术网

Php 改进的number_format()函数,用于处理大于2^53的数字

Php 改进的number_format()函数,用于处理大于2^53的数字,php,limit,number-formatting,Php,Limit,Number Formatting,我为GET请求创建了一个输出数据的API。此数据也会根据用户的区域设置进行格式化。我使用PHPnumber\u format()函数,注意到对于大于2^53的数字,数字格式输出不同的数字(它近似于它们)。这是一个问题,所以我必须创建一个函数来克服这个问题。 请查看示例以了解问题: $original_number = 9223372036854775805.123; echo a_number_format($original_number, 4, ".", "'",3); echo "<

我为GET请求创建了一个输出数据的API。此数据也会根据用户的区域设置进行格式化。我使用PHP
number\u 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
$original_number=9223372036854775805.123;
回显一个_编号格式($original_编号,4,“,”,3);
回声“
”; 回音编号\格式($原始\编号,4,“,”); //产出: 9'223'372'036'854'775'805.1230 9'223'372'036'854'775'808.0000 //请注意,number_format()为大于2^53的任何数字返回近似值
函数编号格式($iso格式中的编号,$NOU of decimals=3,$decimals\U separator='.',$WANDING\U separator='',$WANDING\U grouping=3){
//检查输入变量
如果(!是数字($iso格式的数字)){
错误日志(“警告!my_number_format()函数中提供的参数类型错误。iso_格式的参数\$number_不是数字。”);
返回false;
}
如果(!是数字($no_of_decimals)){
错误日志(“警告!my_number_format()函数中提供的参数类型错误。参数\$no_of_decimals不是数字。”);
返回false;
}
如果(!是数字($digits\u grouping)){
错误日志(“警告!my_number_format()函数中提供的参数类型错误。参数\$digits\u分组不是数字。”);
返回false;
}
//准备变量
$no_of_decimals=$no_of_decimals*1;
//分解点符号后接收的字符串(这是小数的ISO分隔符)
$aux=分解(“.”,iso格式的$number_);
//提取小数部分和整数部分
$integer_part=$aux[0];
$decimal_part=isset($aux[1])?$aux[1]:“”;
//提取负号
$sign='';
if(strpos($integer_part,“-”)==0){
$sign='-';
$integer\u part=substr($integer\u part,1);
}
//调整小数部分(增加或减少小数部分)
如果($0位小数中无位小数>0){
//检查小数部分的实际尺寸
//如果其长度小于小数位数,则添加尾随的零,否则将其四舍五入
if(strlen($decimal\u part)<$no\u of \u decimals){
$decimal_part=str_pad($decimal_part,$no_of_decimals,“0”);
}否则{
$decimal_part=substr($decimal_part,0,$no_of_decimals);
}
}否则{
//如果$no_of_decimals是负数,则完全消除小数
$decimals_分隔符=“”;
$decimal_part='';
}
//格式化整数部分(数字分组)
如果($digits\u grouping>0){
$aux=STREV($integer\U部分);
$integer_part='';
对于($i=strlen($aux)-1;$i>=0;$i--){
如果($i%$digits\u分组==0&&$i!=0){
$integer_part.=“{$aux[$i]}{$数千_separator}”;
}否则{
$integer_part.=$aux[$i];
}
}
}
$processed_number=“{$sign}{$integer_part}{$decimals_separator}{$decimals_part}”;
返回$U编号;
}

对否决票有何评论?
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;
}