The functions to get decimal places in PHP include sprintf, ceil, floor, round and other functions to achieve rounding. Let’s take a look at specific examples.
This article will use PHP to round numbers to N decimal places, as well as use PHP to round numbers to make a brief summary.
(1)php retains three decimal places and rounds
The code is as follows
代码如下 |
复制代码 |
$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022
|
|
Copy code
|
$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022
代码如下 |
复制代码 |
$num=0.0215489;
echo substr(sprintf("%.4f", $num),0,-1); // 0.021 |
代码如下 |
复制代码 |
echo ceil(4.3); // 5
echo ceil(9.999); // 10
|
(2)php retains three decimal places and does not round
The code is as follows
代码如下 |
复制代码 |
echo floor(4.3); // 4
echo floor(9.999); // 9
|
|
Copy code
|
$num=0.0215489;
echo substr(sprintf("%.4f", $num),0,-1); // 0.021
代码如下 |
复制代码 |
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
?>
|
|
(3) PHP takes an integer by one step (this will be used in the page number program of the paging program)
The code is as follows
|
Copy code
echo ceil(4.3); // 5
echo ceil(9.999); // 10
(4) PHP rounding method to get an integer
The code is as follows
|
Copy code
|
echo floor(4.3); // 4
echo floor(9.999); // 9
(5),round function
Example 1. round() example
The code is as follows
|
Copy code
|
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<🎜>
?>
http://www.bkjia.com/PHPjc/634548.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/634548.htmlTechArticleThe functions to get decimal places in php include sprintf, ceil, floor, round and other functions to achieve rounding. Below we Let’s take a look at specific examples. This article will use php to quadruple numbers...
|
|
|
|
|