php method to remove decimal places: 1. Use the intval() function to get the integer value of the variable, the syntax "intval($number)"; 2. Use the floor() function to round down, the syntax " floor($number)”.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes decimal places Method
Method 1: Use the intval() function
The intval() function is used to obtain the integer value of a variable. Syntax format:
int intval ( mixed $var [, int $base = 10 ] )
$var: The quantity value to be converted to integer.
#$base: The base used for conversion.
If base is 0, determine the base used by detecting the format of var:
If the string includes "0x" (or "0X") Prefix, use hexadecimal (hex); otherwise,
If the string starts with "0", use octal (octal); otherwise,
Decimal will be used.
The intval() function is often used to convert data types, converting string and floating-point type variables into integer types.
Example:Convert decimal to integer (remove decimal places)
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = intval($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; var_dump($int); ?>
Output result:
Method 2: Use the floor() function--round down
The floor() function rounds down to the nearest integer. Syntax format:
floor($number);
$number: required. Specifies the value to be rounded down.
Example:Round decimals (remove decimal places)
<?php header("Content-type:text/html;charset=utf-8"); $num = 123.456; $int = floor($num); echo '变量 $int 的类型为:'.gettype($int).'<br>'; var_dump($int); ?>
Output result:
Supplement: Two other methods of decimal rounding
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); ?>
Output result:
<?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); ?>
PHP Video Tutorial"
The above is the detailed content of How to remove decimal places in php. For more information, please follow other related articles on the PHP Chinese website!