Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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 如何用零舍入/格式化长数字?_Php_Math_Numbers_Format_Rounding - Fatal编程技术网

Php 如何用零舍入/格式化长数字?

Php 如何用零舍入/格式化长数字?,php,math,numbers,format,rounding,Php,Math,Numbers,Format,Rounding,我有各种各样的长数字,我试图写一个函数来正确格式化它们。有人能帮我吗 我已经尝试过“number\u format()”和“round()”但这并不能解决我的问题 我想把它概括如下: 1024.43 --> 1,024.43 0.000000931540 --> 0.000000932 0.003991 --> 0.00399 0.3241 --> 0.324 1045.3491 --> 1,045.35 function customRound($

我有各种各样的长数字,我试图写一个函数来正确格式化它们。有人能帮我吗

我已经尝试过“
number\u format()
”和“
round()
”但这并不能解决我的问题

我想把它概括如下:

1024.43  --> 1,024.43  
0.000000931540 --> 0.000000932  
0.003991 --> 0.00399  
0.3241 --> 0.324
1045.3491 --> 1,045.35
function customRound($value)
{
   if ($value > -1 && $value < 1) {

       // define the number of significant digits needed
       $digits = 3;

       if ($value >= 0) {

           // calculate the number of decimal places to round to
           $decimalPlaces = $digits - floor(log10($value)) - 1;
       } else {

           $decimalPlaces = $digits - floor(log10($value * -1)) - 1;
       }

       // return the rounded value
       return number_format($value, $decimalPlaces);

   } else {

      // simply use number_format function to show upto 2 decimal places
      return number_format($value, 2);
    } 

    // for the rest of the cases - return the number simply
    return $value;
}
这意味着,如果数字大于“0”,则应舍入到小数点后2位,并添加千位分隔符(如6554.24),如果数字小于“1”,则每当数字出现在零之后时,应舍入到3位(例如0.0003219到0.000322或0.2319到0.232)

编辑: 这同样适用于“-”值。例如:

-1024.43  --> -1,024.43  
-0.000000931540 --> -0.000000932  
-0.003991 --> -0.00399  
-0.3241 --> -0.324
-1045.3491 --> -1,045.35
改编自

  • 在两种不同的情况下处理此问题
  • 对于介于-1和1之间的数字;我们需要计算要舍入的位数。然后,利用该函数可以得到结果
  • 对于else,只需使用()函数并将十进制数字设置为2
请尝试以下操作:

1024.43  --> 1,024.43  
0.000000931540 --> 0.000000932  
0.003991 --> 0.00399  
0.3241 --> 0.324
1045.3491 --> 1,045.35
function customRound($value)
{
   if ($value > -1 && $value < 1) {

       // define the number of significant digits needed
       $digits = 3;

       if ($value >= 0) {

           // calculate the number of decimal places to round to
           $decimalPlaces = $digits - floor(log10($value)) - 1;
       } else {

           $decimalPlaces = $digits - floor(log10($value * -1)) - 1;
       }

       // return the rounded value
       return number_format($value, $decimalPlaces);

   } else {

      // simply use number_format function to show upto 2 decimal places
      return number_format($value, 2);
    } 

    // for the rest of the cases - return the number simply
    return $value;
}
函数customRound($value)
{
如果($value>-1&&$value<1){
//定义所需的有效位数
$digits=3;
如果($value>=0){
//计算要四舍五入的小数位数
$decimalPlaces=$digits-floor(log10($value))-1;
}否则{
$decimalPlaces=$digits-floor(log10($value*-1))-1;
}
//返回四舍五入的值
返回编号\u格式($value,$decimalPlaces);
}否则{
//只需使用number_format函数即可显示最多2位小数
返回编号\u格式($value,2);
} 
//对于其余的情况,请简单地返回数字
返回$value;
}
输出:

123.45
1.23
0.0123
0.0000123
0.000000123
0.00000000123
基本上,这始终保持最小的2位小数,最多3位有效数字

但是,由于浮点在内部的处理方式(2的幂而不是10的幂),因此存在一些捕获。像
0.1
0.001
等数字无法精确存储,因此它们实际上存储为
0.0999999…
或类似的东西。在这种情况下,它可能看起来是在计算错误的东西,并给你的答案比它应该有更多的有效数字

您可以尝试通过允许公式有误差来抵消这种现象:

number_format($x, max(2, 3 - ceil(log10(abs($x))) - 1e-8))

但这可能会导致其他不良影响。你必须进行测试。

@MadhurBhaiya-Nope,因为这个问题是有效数字,这意味着
111
变成
110
,而OP不想要。OP的数字格式似乎是一种非常独特和专门的格式,我无法理解他为什么需要:)@Davіd我们可以轻松地使用该函数,并在数字>0和数字<0时更改其处理方式。对于
>0
情况,
number\u格式就足够了;还有自定义函数吗?你有没有自己尝试过的?plz POST我认为这个问题将小于和大于1的数字分开,而不是0。我说的对吗?@MadhurBhaiya所以你承认这不是一个完全相同的副本?此外,进一步阅读副本:“如果问题有相同的(潜在的)答案,那么它们可能是副本。这不仅包括逐字逐句的副本,还包括用不同的词表达的相同想法。”谢谢。到目前为止,这很好,而且似乎有效。只有两个问题:如果数字太小,它会输出这样的“8.54E-5”,这个脚本也能处理负数吗?谢谢大家!@N1njaWTF检查编辑的答案。它应该处理大于0的非常低的值。它应该如何寻找负数?请编辑您的问题,以显示负数的示例输出,也请参见!这非常有效。我已编辑了我的答案。:)如果数字前有一个“-”,格式基本相同。谢谢。@N1njaWTF请检查更新的答案。我还添加了rextester演示。对你来说应该很好:-)伟大的人!!完美的作品!我做了一个小编辑,所以它也输出了数字“0”:)非常感谢你,伙计!