gpt4 book ai didi

php - 想要显示 float 后的 2 位数字

转载 作者:可可西里 更新时间:2023-11-01 13:09:34 25 4
gpt4 key购买 nike

我想将浮点后 8 位数的浮点值转换为浮点后 2 位数..

例如。 $a = 2.200000 ==> 2.20

我正在使用 php 的 round 函数。 round 的问题是,如果我的数字是 2.200000,它会将数字转换为 2.2。我希望输出为 2.20

谁能推荐一下可行的方法?

实际代码

$price = sprintf ("%.2f", round(($opt->price_value + ($opt->price_value * $this->row->prices[0]->taxes[0]->tax_rate)), 2));

如果我的 float 是 2.2000000,我想输出。那么它应该返回我 2.20。但现在它返回我 2.2

最佳答案

这就是我认为你所要求的:

<?php

$a = 2.20032324;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";

$a = 2.2000000;
$f = sprintf ("%.2f", $a);
echo "$a rounded to 2 decimal places is '$f'\n";

结果:

[wally@lenovoR61 ~]$ php t.php
2.20032324 rounded to 2 decimal places is '2.20'
2.2 rounded to 2 decimal places is '2.20'

我添加了两个测试用例

关于php - 想要显示 float 后的 2 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com