我如何在 php 中舍入并保留两个零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7493305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:48:20  来源:igfitidea点击:

how do i round in php with keeping the two zeros

phprounding

提问by Matt Elhotiby

I have this php

我有这个 php

<?php echo round($price, 2); ?>

and the $pricemaybe 1.0000

$price可能1.0000

i want 1.00but i get only 1

我想要,1.00但我只得到1

any ideas

有任何想法吗

回答by John Godspeed

number_formatworks:

number_format作品:

echo number_format($price, 2);

echo number_format($price, 2);

回答by Tim Cooper

The following printf()call should work for you:

以下printf()电话应该适合您:

<?php printf("%.2f", $price); ?>

The documentation for this syntax is best described on the sprintf()page.

此语法的文档最好在sprintf()页面上进行描述。

回答by Louis

number_formatis your best bet.

number_format是您最好的选择。

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

Example:

例子:

<?php echo number_format(1.0000, 2, '.', ','); ?>