Home  >  Article  >  Backend Development  >  PHP maintains two decimal places

PHP maintains two decimal places

不言
不言Original
2018-04-16 14:09:075100browse

The main content of this article is about keeping two decimal points in PHP, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

I have been doing statistics recently. I have a lot of exposure to data about numbers.

uses three functions to retain N digits after the decimal. Next, I will briefly introduce the three functions:

1, number_format

echo number_format("5000000")."<br>";
echo number_format("5000000",2)."<br>";
echo number_format("5000000",2,",",".");

The main function of this function is to implement: thousandth grouping formatted data.

Output result

5,000,000
5,000,000.00
5.000.000,00

2, sprintf

$num = 123;
$data=sprintf("%.2f",$num);

echo $data;

The result is:

123.00

3, round


$num1 = 123.1267
$data1 = round($num1, 2);

$num2 = 123
$data2 = round($num2, 2);

echo $data1."<br>"
echo $data2


The result is:

123.13

123

If this function has less than two decimal points, it will not automatically help you Complete.

Related recommendations:

How to reverse Chinese strings in PHP to avoid garbled characters

The above is the detailed content of PHP maintains two decimal places. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn