Home  >  Article  >  Backend Development  >  How to convert php string to floating point data

How to convert php string to floating point data

青灯夜游
青灯夜游Original
2021-10-28 19:04:064736browse

Conversion method: 1. Add the target type "(float)", "(double)" or "(real)" enclosed in parentheses before the string variable; 2. Use "floatval(character String)" statement; 3. Use the "settype(string variable, "float")" statement.

How to convert php string to floating point data

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php string conversion For floating-point data

Method 1: Add the target type enclosed in parentheses "(float)", "before the string variable (double)" or "(real)"

  • ##(float), (double), (real): will The target variable is converted to a floating point type;

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456abc&#39;;
$float1 = (float)$str;
echo &#39;变量 $float1 的类型为:&#39;.gettype($float1).&#39;<br>&#39;;
$float2 = (double)$str;
echo &#39;变量 $float2 的类型为:&#39;.gettype($float2).&#39;<br>&#39;;
$float3 = (real)$str;
echo &#39;变量 $float3 的类型为:&#39;.gettype($float3).&#39;<br>&#39;;
?>

Output result:


How to convert php string to floating point data

##Method 2: Use the floatval() function

floatval(): used to obtain the floating point value of the variable;

<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456abc&#39;;
$float = floatval($str);
echo &#39;变量 $float 的类型为:&#39;.gettype($float).&#39;<br>&#39;;
?>

Output result:

How to convert php string to floating point data

Method 3: Use the settype() function

The settype() function is used to set the type of a variable.

Syntax:

settype (mixed &$var, string $type)


$type: The possible values ​​of type are.

    "boolean" (or "bool" since PHP 4.2.0)
  • "integer" (or "int" , starting from PHP 4.2.0)
  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)
  • "string"
  • "array"
  • "object"
  • "null" (since PHP 4.2.0)
  • Example:
<?php
header("Content-type:text/html;charset=utf-8");
$str = &#39;123.456&#39;;
$float = settype($str, &#39;float&#39;);;
echo &#39;$str 的类型为:&#39;.gettype($str).&#39;<br> $float 的类型为:&#39;.gettype($float);
?>

Output:


How to convert php string to floating point dataNote: The settype() function will change the original type of the variable. If the setting is successful, it will return TRUE and if it fails, it will return FALSE.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to convert php string to floating point data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn