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

Four methods commonly used in PHP integer function:
1. Direct rounding, discarding decimals, and keeping integers: intval();
2. Rounding up: round();
3. Rounding up, if there is a decimal, add 1: ceil( );
4. Round down: floor().

1. intval - if the variable is converted into an integer type
intval, it will be automatically converted to 0 if it is a character type. Usually, some people use it to force a numeric type, but it should be noted that if the length is too long, it is recommended to use (int).

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

2. Rounding: round()
rounds parameter 1 according to the precision specified by parameter 2. Argument 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, 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

Fourth, 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686969&siteId=291194637