Home  >  Article  >  Backend Development  >  Summary of PHP integer division and rounding methods

Summary of PHP integer division and rounding methods

藏色散人
藏色散人Original
2020-08-26 10:13:208940browse

php integer division and rounding methods: 1. Use the "intval" method to convert the variable into an integer type; 2. Use the "round" method to round; 3. Use the "ceil" method to round up; 4. Use the "floor" method to round down.

Summary of PHP integer division and rounding methods

# Recommended: "

PHP Video Tutorial

Four commonly used methods for PHP integer function:

1. Round directly, discard decimals, and keep integers: intval();

2. Rounding: round();
3. Rounding up, if there are decimals, add 1: ceil();
4. Rounding down: floor().

1. intval—convert the variable into an integer type

If intval is a character type, it will be automatically converted to 0.

intval(3.14159);  // 3
intval(3.64159);  // 3
intval('ruesin'); //0

2. Rounding: round()

Round parameter 1 according to the precision specified by parameter 2. Parameter 2 can be negative or zero (default).

round(3.14159);      // 3
round(3.64159);      // 4
round(3.64159, 0);   // 4
round(3.64159, 2);   // 3.64
round(5.64159, 3);   // 3.642
round(364159, -2);   // 364200

3. Round up and add 1 if there is a decimal: ceil()

Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up by one.

This method is often used when we write paging classes to calculate the number of pages.

ceil(3.14159);  // 4
ceil(3.64159);  // 4

4. Round down: floor()

Returns the next integer that is not greater than value, and rounds the decimal part of value.

floor(3.14159);    // 3
floor(3.64159);    // 3

The above is the detailed content of Summary of PHP integer division and rounding methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn