Home >Backend Development >PHP Problem >How to convert integer to floating point in php
php method to convert integer type to floating point type: 1. Use the floatval() function to obtain the floating point value of the variable. The syntax is "floatval (integer value)"; 2. Use settype( ) function, which can convert variables into specified data types. The syntax is "settype(integer value, "float")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will integer Convert to floating point
Method 1: Use the floatval() function
The floatval function is used to obtain the floating point value of a variable.
floatval cannot be used with arrays or objects.
<?php header("Content-type:text/html;charset=utf-8"); $int = 547; echo '变量的原类型为:'; var_dump($int); $float = floatval($int); echo '变量转换后的类型为:'; var_dump($float); ?>
Method 2: settype() function
settype ($var,$type)
function Used to set variable $var
to the specified data type $type
.
<?php header("Content-type:text/html;charset=utf-8"); $int = 547; echo '变量的原类型为:'; var_dump($int); settype($int,"float"); echo '变量转换后的类型为:'; var_dump($int); ?>
Note: The value of
# The settype() function will change the original variable. Recommended learning: "$type can be:
"boolean" ( or "bool", as of PHP 4.2.0)
- ##"integer" (or "int", as of PHP 4.2.0)
- "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
- "string"
- "array"
- "object"
- "null" (from PHP 4.2.0)
PHP Video Tutorial"
The above is the detailed content of How to convert integer to floating point in php. For more information, please follow other related articles on the PHP Chinese website!