Home  >  Article  >  Backend Development  >  The use of rounding, rounding up, rounding down, and decimal interception in PHP division operations

The use of rounding, rounding up, rounding down, and decimal interception in PHP division operations

不言
不言Original
2018-04-23 14:42:364984browse

This article mainly introduces the use of rounding, rounding up, rounding down, and decimal interception in PHP division operations. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Four commonly used methods for PHP integer functions:
1. Round directly, discard decimals, and retain integers: intval();
2. Rounding to integers: round();
3. Round up and add 1 if there are decimals: ceil();
4. Round down: floor().

1. intval—convert variables into integer types
If intval is a character type, it will be automatically converted to 0. Usually some people use it to force conversion to a numeric type, but you need to pay attention to the situation when the length is too long. Below, it is recommended to use (int).

intval(5.2);  // 5
intval(5.6);  // 5
intval('abc'); //0

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

round(5.2);      // 5
round(5.8);      // 6
round(5.88888, 0);   // 6
round(5.83333, 2);   // 5.83
round(5.83353, 3);   // 5.834
round(5201314, -2);  //5201300

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

echo(ceil(0.60);   //1
echo(ceil(0.40);   //1
echo(ceil(5);      //5
echo(ceil(5.1);    //6
echo(ceil(-5.1);   //-5
echo(ceil(-5.9));  //-5

4. Round down: floor()

echo(floor(0.60));  //0
echo(floor(0.40));  //0
echo(floor(5));     //5
echo(floor(5.1));   //5
echo(floor(-5.1));  //-6
echo(floor(-5.9))   //-6

 

The above is the detailed content of The use of rounding, rounding up, rounding down, and decimal interception in PHP division operations. 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