固定为 0.2 小数

发布于 2024-12-18 00:13:30 字数 412 浏览 0 评论 0原文

可能的重复:
PHP 中默认输出的小数位数 < /p>

基本上有点数学问题,

$average_ppm = $total_points_given / $totalvalue;

$average_ppm 现在等于 2.432608695652174,我不想显示这些数字,我只需要 $average_ppm 2.43,因此固定到小数点后 2 位。我该怎么做?

谢谢大家的时间。

Possible Duplicate:
Default Number of Decimal Places to Output in PHP

basically a bit of a maths problem,

$average_ppm = $total_points_given / $totalvalue;

$average_ppm now equals 2.432608695652174, I don't want to display these numbers, I just need $average_ppm to be 2.43, so to a fixed 2 decimal points. How can I do this??

Thanks for anyones time.

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

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

发布评论

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

评论(5

池予 2024-12-25 00:13:30

如果您想要字符串输出,请使用 sprintf ,或者 圆形/地板/ceil 对于数值:

$average_ppm = 2.432608695652174;

echo sprintf("%.2f", $average_ppm); // 2.43

$approx_average_ppm = round($average_ppm, 2);
echo $approx_average_ppm; // 2.43

echo floor($average_ppm, 2); // 2.43 , even if $average_ppm = 2.439
echo ceil($average_ppm, 2); // 2.44

Use sprintf if you want a string output, or round/floor/ceil for a numeric value:

$average_ppm = 2.432608695652174;

echo sprintf("%.2f", $average_ppm); // 2.43

$approx_average_ppm = round($average_ppm, 2);
echo $approx_average_ppm; // 2.43

echo floor($average_ppm, 2); // 2.43 , even if $average_ppm = 2.439
echo ceil($average_ppm, 2); // 2.44
魂牵梦绕锁你心扉 2024-12-25 00:13:30

您可以使用 sprintf圆形地板/ceil 取决于你如何想要数字四舍五入。

最适合您需要的是圆形:

$average_ppm = round($total_points_given / $totalvalue,2);

You could either use sprintf, round or floor/ceil depending on how you want the numbers rounded.

Most suited for your need would be round:

$average_ppm = round($total_points_given / $totalvalue,2);
旧话新听 2024-12-25 00:13:30

如果你想在...之后始终有 2 个数字,你可以使用 number_format< /a>:

number_format(2.43260869565217, 2); // 2.43

当你得到一个像这样的数字:2.400054846 并且你使用 round 你会得到 2.4
如果你想要后面有 2 个数字,你可以使用 number_format 这将输出 2.40

If you want to have ALWAYS 2 numbers after... you can do it with number_format:

number_format(2.43260869565217, 2); // 2.43

When you got a number like: 2.400054846 and you use round you will get 2.4
and if you want it with 2 number behind you can use number_format this will output 2.40

云朵有点甜 2024-12-25 00:13:30

我会使用 Round

round($average_ppm, 2);

I would use Round:

round($average_ppm, 2);
痴梦一场 2024-12-25 00:13:30

如果第三个参数可用,您可以使用 bcmath 函数精度

$average_ppm = bcdiv($total_points_given, $totalvalue, 2);

You could use the bcmath functions if they are available where the third argument is the precision

$average_ppm = bcdiv($total_points_given, $totalvalue, 2);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文