【问题标题】:Returning a "readable" number in PHP在 PHP 中返回一个“可读”的数字
【发布时间】:2020-03-11 02:06:11
【问题描述】:

我正在尝试接收小数(最多 8 位小数)的数学运算结果,我收到一个浮点结果,但其格式会使其他数字保持错误:

$a = round($x, 8); //returns 0.0478674, that's correct
$b = round($y,8); //returns 0.04786261, that's correct
$z = $a - $b; //z returns 4.7899999999976E-6, and not ‭0.00000479‬ as I was expecting

我也试过了

$w = round($z,8); //but w returns 4.79E-6, and not 0.00000479‬ as I was expecting

我的问题是因为数字4.7899999999976E-6 在其他计算中出现错误,并且它是一个难看的数字,无法显示给用户。

我怎样才能让这个号码成为0.00000479

【问题讨论】:

    标签: php rounding


    【解决方案1】:

    你可以使用number_format:

    $w = number_format($z,8);
    

    【讨论】:

      【解决方案2】:

      number_format 应该满足您的要求,来自帮助:https://www.php.net/manual/en/function.number-format.php

      这里有您的具体要求:

      $w = number_format($z,8);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-23
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        相关资源
        最近更新 更多