Methods for division and rounding in php: 1. Use the [round()] function to round floating point numbers; 2. Use the [ceil()] function to round up to the nearest integer; 3. Use The [floor()] function rounds down to the nearest integer.
How to divide and round in php:
1. round: rounding
round()
The function rounds floating point numbers.
#Description: Returns the result of rounding x according to the specified precision prec (the number of decimal digits after the decimal point). prec can also be negative or zero (default).
Tip: PHP cannot handle strings like "12,300.2" correctly by default.
Example:
<?php echo(round(0.60)); echo(round(0.50)); echo(round(0.49)); echo(round(-4.40)); echo(round(-4.60)); ?>
Output:
1 1 2 1 3 0 4 -4 5 -5
2. ceil: round up
ceil() function rounds up is the nearest integer.
Description: Return the next integer that is not less than x. If x has a decimal part, it will be rounded up by one. The type returned by ceil() is still float because the range of float values is usually larger than that of integer.
Example:
<?php echo(ceil(0.60); echo(ceil(0.40); echo(ceil(5); echo(ceil(5.1); echo(ceil(-5.1); echo(ceil(-5.9)); ?>
Output:
1 1 2 1 3 5 4 6 5 -5 6 -5
3. floor: round down
floor() function down Rounded to the nearest integer.
Syntax: floor(x)
Description: Return the next integer not greater than x, and round off the decimal part of x. The type returned by floor() is still float because the range of float values is usually larger than that of integer.
Example:
<?php echo(floor(0.60)); echo(floor(0.40)); echo(floor(5)); echo(floor(5.1)); echo(floor(-5.1)); echo(floor(-5.9)) ?>
Output:
1 0 2 0 3 5 4 5 5 -6 6 -6
Related learning recommendations: php programming (video)
The above is the detailed content of How to divide and round in php. For more information, please follow other related articles on the PHP Chinese website!