php 将数字显示为两位小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4483540/
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-25 13:10:07  来源:igfitidea点击:

Show a number to two decimal places

phpformattingnumbersroundingnumber-formatting

提问by Rich Bradshaw

What's the correct way to round a PHP string to two decimal places?

将 PHP 字符串四舍五入到两位小数的正确方法是什么?

$number = "520"; // It's a string from a database

$formatted_number = round_to_2dp($number);

echo $formatted_number;

The output should be 520.00;

输出应该是520.00;

How should the round_to_2dp()function definition be?

round_to_2dp()函数定义应该如何?

回答by Codemwnci

You can use number_format():

您可以使用number_format()

return number_format((float)$number, 2, '.', '');

Example:

例子:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00

This function returns a string.

此函数返回一个字符串

回答by Somnath Muluk

Use round()(use if you are expecting a number in float format only, else use number_format() as an answer given by Codemwnci):

使用round()(如果您只需要浮点格式的数字,请使用,否则使用 number_format() 作为Codemwnci 给出的答案):

echo round(520.34345, 2);   // 520.34
echo round(520.3, 2);       // 520.3
echo round(520, 2);         // 520

From the manual:

从手册:

Description:

float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]]);

Returns the rounded value of valto specified precision(number of digits after the decimal point). precisioncan also be negative or zero (default).

描述:

float round(float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]]);

返回val指定的舍入值precision(小数点后的位数)。precision也可以是负数或零(默认)。

...

...

Example #1 round()examples

<?php
    echo round(3.4);         // 3
    echo round(3.5);         // 4
    echo round(3.6);         // 4
    echo round(3.6, 0);      // 4
    echo round(1.95583, 2);  // 1.96
    echo round(1241757, -3); // 1242000
    echo round(5.045, 2);    // 5.05
    echo round(5.055, 2);    // 5.06
?>

Example #2 mode examples

<?php
    echo round(9.5, 0, PHP_ROUND_HALF_UP);   // 10
    echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
    echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
    echo round(9.5, 0, PHP_ROUND_HALF_ODD);  // 9

    echo round(8.5, 0, PHP_ROUND_HALF_UP);   // 9
    echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
    echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
    echo round(8.5, 0, PHP_ROUND_HALF_ODD);  // 9
?>

示例 #1round()示例

<?php
    echo round(3.4);         // 3
    echo round(3.5);         // 4
    echo round(3.6);         // 4
    echo round(3.6, 0);      // 4
    echo round(1.95583, 2);  // 1.96
    echo round(1241757, -3); // 1242000
    echo round(5.045, 2);    // 5.05
    echo round(5.055, 2);    // 5.06
?>

Example #2 模式示例

<?php
    echo round(9.5, 0, PHP_ROUND_HALF_UP);   // 10
    echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
    echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
    echo round(9.5, 0, PHP_ROUND_HALF_ODD);  // 9

    echo round(8.5, 0, PHP_ROUND_HALF_UP);   // 9
    echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
    echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
    echo round(8.5, 0, PHP_ROUND_HALF_ODD);  // 9
?>

回答by Marc B

Alternatively,

或者,

$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00

回答by SystemX17

http://php.net/manual/en/function.round.php

http://php.net/manual/en/function.round.php

e.g.

例如

echo round(5.045, 2);    // 5.05

echo round(5.055, 2);    // 5.06

回答by Biju

Try:

尝试:

$number = 1234545454; 
echo  $english_format_number = number_format($number, 2); 

The output will be:

输出将是:

1,234,545,454.00

回答by Aditya P Bhatt

You can use the PHP printfor sprintffunctions:

您可以使用 PHPprintfsprintf函数:

Example with sprintf:

示例sprintf

$num = 2.12;
echo sprintf("%.3f", $num);

You can run the same without echoas well. Example: sprintf("%.3f", $num);

您也可以在没有的情况下运行相同的程序echo。例子:sprintf("%.3f", $num);

Output:

输出:

2.120

Alternatively, with printf:

或者,使用printf

echo printf("%.2f", $num);

Output:

输出:

2.124

回答by powtac

Another more exotic way to solve this issue is to use bcadd()with a dummy value for the $right_operand of 0.

解决此问题的另一种更奇特的方法是对bcadd()$right_operand使用虚拟值0

$formatted_number = bcadd($number, 0, 2);

回答by Sani Kamal

Use the PHP number_format()function.

使用 PHP number_format()函数。

For example,

例如,

$num = 7234545423;
echo number_format($num, 2);

The output will be:

输出将是:

7,234,545,423.00

回答by Shamim Hafiz

Use the PHP number_format()function.

使用 PHP number_format()函数。

回答by ajreal

round_to_2dpis a user-defined function, and nothing can be done unless you posted the declaration of that function.

round_to_2dp是用户定义的函数,除非您发布该函数的声明,否则无法执行任何操作。

However, my guess is doing this: number_format($number, 2);

但是,我的猜测是这样做: number_format($number, 2);