How to convert floating point numbers in php: 1. Use the floatval() function to get the floating point value of the variable, the syntax is "floatval (data to be converted)"; 2. Use the settype() function, the syntax is " settype(data to be converted, "float")" can convert the specified data to a floating point type.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP provides two built-in functions, which can Convert other types of data to floating point numbers:
floatval() function
settype() function
1. Use the floatval() function
The floatval() function is a function that specifically converts other types into floating point types. It can obtain the floating point value of a variable.
Note: floatval cannot be used for arrays or objects.
<?php header('content-type:text/html;charset=utf-8'); $var = '122.34343runoob'; echo "原数据类型:"; var_dump($var); $float= floatval($var); echo "修改后的类型:"; var_dump($float); ?>
<?php header('content-type:text/html;charset=utf-8'); $var = TRUE; echo "原数据类型:"; var_dump($var); $float= floatval($var); echo "修改后的类型:"; var_dump($float); ?>
2. Use the settype() function
settype( ) function can convert a value to a specified data type (controlled by the second parameter).
You only need to set the second parameter of the function to "float" to convert the data into a floating point number.
Note: This function will modify the original variable; if the setting is successful, it will return TRUE, if it fails, it will return FALSE.
<?php header('content-type:text/html;charset=utf-8'); $foo = "5bar"; // string $bar = true; // boolean echo "原数据类型:"; var_dump($foo); var_dump($bar); settype($foo, "float"); settype($bar, "float"); echo "修改后的类型:"; var_dump($foo); var_dump($bar); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert data to floating point number in php. For more information, please follow other related articles on the PHP Chinese website!