1. ceil - rounding up
Description
float ceil ( float value )
returns the next integer not less than value, and if value has a fractional part, it is rounded up by one. The type returned by ceil() is still float, because the range of float values is usually larger than that of integers.
Example 1. ceil() example
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
?>
2. floor - rounding up
Description
float floor ( float value )
returns the next integer not greater than value, rounding off the fractional part of value. The type returned by floor() is still float, because the range of float values is usually larger than that of integers.
Example 1. floor() example
<?php
echo floor(4.3); // 4
echo floor(9.999); // 9
?>
3. round - rounds floating point numbers
DESCRIPTION
float round ( float val [, int precision] )
returns the result of rounding val to the specified precision precision (the number of digits after the decimal point). precision can also be negative or zero (default).
Example 1. round() example
<?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
?>
Fourth, intval - convert the variable to an integer type
Example intval()
<?php
echo intval(4.3); //4
echo intval(4.6); // 4
?>
PHP rounding to exact decimal places and rounding
(1) php retains three decimal places and rounds up
$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022
(2) php retains three decimal places without rounding
$num=0.0215489;
echo substr(sprintf("%.4f", $num),0,-1); // 0.021
(3) php takes an integer by one method (this will be used in the page number program of the paging program)
echo ceil(4.3); // 5
echo ceil(9.999); // 10
(4) php rounding method to take an integer
echo floor(4.3); // 4
echo floor(9.999); // 9
(5), round function
Example 1. round() example
<?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
?>
PHP rounding the most accurate way to two decimal places
<?php
$number = 123213.066666;
echo sprintf("%.2f", $number);
?>
Output result:
123213.07