【问题标题】:How do I get rid of php decimals我如何摆脱php小数
【发布时间】:2021-08-21 19:48:08
【问题描述】:

我正在尝试使用这个简单的 php 表达式计算 BMI:

$bmi = $weight/pow($height, 2);

体重:70 身高:170 它输出:0.0024221453287197

但正确的结果必须是:24.22,如何去掉所有这些小数和零,谢谢。

【问题讨论】:

标签: php rounding


【解决方案1】:

这是怎么回事..

$num = 0.0024221453287197;

// move the decimal right 4 places.
$num = $num * 10000;

// format the number
echo number_format($num, 2);

http://sandbox.onlinephpfunctions.com/code/b95dd43a426b5d55bf1fddd9faf7b13116908192 测试。

【讨论】:

    【解决方案2】:

    只需将输入除以 100。

    echo $weight/pow($height/100, 2); // 24.221453287197

    在线测试:https://3v4l.org/VIh0r

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2023-03-31
      • 2011-10-02
      • 2019-09-06
      • 1970-01-01
      相关资源
      最近更新 更多