In php, the division method (round, ceil, floor) and ceilfloor
In PHP, you need to use the following method to get the result of division:
1. round: Rounding
The round () function rounds a floating point number.
Syntax: round (x, prec)
Parameters |
Description |
X |
Optional. Specifies the number to be rounded. |
Prec |
Optional. Specifies the number of digits after the decimal point. |
Returns the result of rounding x to the specified precision prec (number of digits after decimal point. Prec can also be a negative number or zero (default ).
Tip: by default, PHP cannot correctly process strings similar to "12,300.2.
Example:
1 <?php2 echo(round(0.60));3 echo(round(0.50));4 echo(round(0.49));5 echo(round(-4.40));6 echo(round(-4.60));7 ?>
Output:
1 12 13 04 -45 -5
2. ceil: rounded up
The ceil () function rounds up to the nearest integer.
Syntax: ceil (x)
Parameters |
Description |
X |
Required. Specifies the number to be rounded. |
Description: return the next integer not less than x. If x has a decimal part, the return value is one digit. The type returned by ceil () is still float, because the float value range is usually larger than integer.
Example:
1 <?php2 echo(ceil(0.60);3 echo(ceil(0.40);4 echo(ceil(5);5 echo(ceil(5.1);6 echo(ceil(-5.1);7 echo(ceil(-5.9));8 ?>
Output:
1 12 13 54 65 -56 -5
3. floor: round down
The floor () function rounds down to the nearest integer.
Syntax: floor (x)
Parameters |
Description |
X |
Required. Specifies the number to be rounded. |
Note: the return value is the next integer not greater than x, and the decimal part of x is rounded out. The return type of floor () is still float, because the float value range is usually larger than that of integer.
Example:
1 <?php2 echo(floor(0.60));3 echo(floor(0.40));4 echo(floor(5));5 echo(floor(5.1));6 echo(floor(-5.1));7 echo(floor(-5.9))8 ?>
Output:
1 02 03 54 55 -66 -6