PHP 如何四舍五入到小数点后两位

发布于 2024-10-24 18:46:45 字数 327 浏览 4 评论 0原文

我想在 PHP 中将数字四舍五入到小数点后两位。

PHP 代码:

$bmi = ($form_state[values][submitted][1] * 703) / 
    ($form_state[values][submitted][10] * $form_state[values][submitted][10]);
$form_values['submitted'][11] = $bmi;
$form_values['submitted_tree'][11] = $bmi;

舍入变量 $bmi 的最佳方法是什么?

I would like to round a number to two decimal places in PHP.

PHP code:

$bmi = ($form_state[values][submitted][1] * 703) / 
    ($form_state[values][submitted][10] * $form_state[values][submitted][10]);
$form_values['submitted'][11] = $bmi;
$form_values['submitted_tree'][11] = $bmi;

What is the best way to round the variable $bmi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ记忆︶ㄣ 2024-10-31 18:46:45

Edit3:

好吧,如果我正确理解你的代码,它应该是这样的:

<?php
    //Calculate $bmi
    $bmi = ($form_state[values][submitted][1] * 703) / 
       ($form_state[values][submitted][10] * $form_state[values][submitted][10]);

    //Fill unrounded $bmi var into array for whatever reason
    $form_values['submitted'][11] = $bmi;

    //Fill unrounded $bmi var into array for whatever reason
    $form_values['submitted_tree'][11] = $bmi;

    //$bmi contains for example 24.332423
    $bmi = round($bmi,2);

    //Should output 24.33
    echo $bmi;
?>

如果出现任何问题,我只能假设计算出的 $bmi var 在某个地方搞砸了。

是否假设您将未四舍五入的值填充到 $form_values['subscribed'][11] 中?如果没有尝试以下操作:

 $bmi = ($form_state[values][submitted][1] * 703) / ($form_state[values][submitted][10] *  
 $bmi = round($bmi,2); 
 $form_values['submitted'][11] = $bmi;
 $form_values['submitted_tree'][11] = $bmi;

Edit3:

Well if I understand your code right it should go like this:

<?php
    //Calculate $bmi
    $bmi = ($form_state[values][submitted][1] * 703) / 
       ($form_state[values][submitted][10] * $form_state[values][submitted][10]);

    //Fill unrounded $bmi var into array for whatever reason
    $form_values['submitted'][11] = $bmi;

    //Fill unrounded $bmi var into array for whatever reason
    $form_values['submitted_tree'][11] = $bmi;

    //$bmi contains for example 24.332423
    $bmi = round($bmi,2);

    //Should output 24.33
    echo $bmi;
?>

If anything goes wrong I can only assume that the calculated $bmi var gets messed up somewhere.

Is it supposed that you fill the unrounded value into $form_values['submitted'][11]? if not try the following:

 $bmi = ($form_state[values][submitted][1] * 703) / ($form_state[values][submitted][10] *  
 $bmi = round($bmi,2); 
 $form_values['submitted'][11] = $bmi;
 $form_values['submitted_tree'][11] = $bmi;
生生漫 2024-10-31 18:46:45

PHP round 将采用一个数字并将其四舍五入到 2 个单位的精度:

$foo = 105.5555;
echo round($foo, 2);                    //prints 105.56

或者一个字符串并将其四舍五入到 2 个单位的精度:

$bmi = "52.44444";
echo round($bmi, 2);                    //prints 52.44

四舍五入到两个单位的精度,并以这种方式强制使用最小精度:

$foo = 105.5555;
echo number_format(round($foo, 2), 4, '.', '');  //prints 105.5600

PHP round will take a number and round it to 2 units precision:

$foo = 105.5555;
echo round($foo, 2);                    //prints 105.56

Or a string and round it to 2 units precision:

$bmi = "52.44444";
echo round($bmi, 2);                    //prints 52.44

Round to two units precision, and force a minimum precision this way:

$foo = 105.5555;
echo number_format(round($foo, 2), 4, '.', '');  //prints 105.5600
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文