Method: 1. Use intval() function, syntax "intval($num)"; 2. Use ceil() function, syntax "ceil($num)"; 3. Use floor() function, The syntax is "floor($num)"; 4. Use the round() function, the syntax is "round($num)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php decimal rounding Method
#1. Use the intval() function
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = intval($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; var_dump($int); ?>
intval() function is used to obtain The integer value of the variable; this function is often used for data type conversion to convert string and floating-point type variables into integer types.
2. Use the ceil() function - round up, if there is a decimal, add 1 to the integer part
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = ceil($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; var_dump($int); ?>
3. Use the floor() function--round down
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = floor($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; var_dump($int); ?>
4. Use the round() function--round up
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = round($num); echo '小数值为:'.$num.'<br>'; echo '取整后值为:'.$int .'<br>'; var_dump($int); $num = 123.556; $int = round($num); echo '小数值为:'.$num.'<br>'; echo '取整后值为:'.$int .'<br>'; var_dump($int); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the methods for rounding decimals in PHP?. For more information, please follow other related articles on the PHP Chinese website!