How to convert float to int type in PHP: 1. Use forced type conversion and add the target type "(int)" enclosed in parentheses before the float variable to be converted, for example " (int)3.14"; 2. Use the intval() function to obtain the integer value of the variable.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will convert float Into type int
#1. Forced type conversion--add the target type enclosed in parentheses before the variable to be converted
allowed The converted PHP data types are:
(int), (integer): converted to integer
(float), (double), (real): Convert to floating point type
(string): Convert to string
(bool), (boolean): Convert Into Boolean type
(array): Convert to array
(object): Convert to object
Example: Convert float type to int type
<?php $num1=3.14; $num2=(int)$num1; var_dump($num1); //输出float(3.14) var_dump($num2); //输出int(3) ?>
Output:
[Recommended learning: "PHP video tutorial 》]
2. Use the intval() function
<?php $num1=123.9; $num2=intval($num1); var_dump($num1); var_dump($num2); ?>
Output:
##intval( ) function is used to obtain the integer value of a variable. intval() function returns the integer value of variable var by using the specified base conversion (default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned. Syntax:int intval ( mixed $var [, int $base = 10 ] )
Expand knowledge:
intval(39.80*100) = 3979 intval(round(floatval($value) * 100)) = 3980
For more programming related knowledge, please visit:
Programming Video! !
The above is the detailed content of How to convert float to int type in php. For more information, please follow other related articles on the PHP Chinese website!