When you encounter a situation in PHP that requires the results of division to be rounded, you need to use the following methods:
1. Round: Rounding
The round () function rounds a floating-point number.
Syntax: Round (x, PREC)
Parameters |
Describe |
X |
Optional. Specifies the number to round. |
Prec |
Optional. Specifies the number of digits after the decimal point. |
Description: Returns the result of rounding x according to the specified precision Prec (number of digits after decimal point). Prec can also be negative or 0 (the default value).
Tip: PHP does not correctly handle strings like "12,300.2" by default.
Cases:
1 <? Php2 echo (round (0.60< Span style= "color: #000000")); 3 echo (round (0.504 echo (round (0.495 echo (round ( -4.406 echo (round ( -4.607,
Output:
1 12 13 04-45-5
2. Ceil: Rounding up
The Ceil () function rounds up to the nearest integer.
Syntax: Ceil (x)
Parameters |
Describe |
X |
Have to. Specifies the number to round. |
Note: Returns the next integer that is not less than x, or x if there are fractional parts. The type returned by Ceil () is still float, because the range of float values is usually larger than the integer.
Cases:
1 <?Php2echo (ceil (0.603 echo (ceil (0.40); 4 echo (ceil (55 echo (ceil (5.16 echo (ceil ( -5.17 echo (ceil ( -5.98,
Output:
1 12 13 54 65-56-5
3. Floor: Rounding Down
The floor () function rounds down to the nearest integer.
Syntax: Floor (x)
Parameters |
Describe |
X |
Have to. Specifies the number to round. |
Description: Returns the next integer not less than x, and the fractional part of X is rounded off. The type returned by floor () is still float, because the range of float values is usually larger than the integer.
Cases:
1 <?Php2echo (floor (0.603 echo (floor (0.404 echo (floor (55 echo (floor (5.16 echo (floor ( -5.17 echo (floor ( -5.98?
Output:
1 02 03 54 55-66-6
Method of division Rounding in PHP (Round,ceil,floor)