PHP maintains two decimal places

不言
Release: 2023-03-24 06:00:02
Original
5045 people have browsed it

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,",",".");
Copy after login

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;
Copy after login

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
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!