Home  >  Article  >  Backend Development  >  How to convert integer to floating point in php

How to convert integer to floating point in php

青灯夜游
青灯夜游Original
2022-03-11 19:58:024041browse

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")".

How to convert integer to floating point in php

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 &#39;变量的原类型为:&#39;;
    var_dump($int);
    $float = floatval($int);
    echo &#39;变量转换后的类型为:&#39;;
    var_dump($float);
?>

How to convert integer to floating point in php

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 &#39;变量的原类型为:&#39;;
var_dump($int);
settype($int,"float");
echo &#39;变量转换后的类型为:&#39;;
var_dump($int);
?>

How to convert integer to floating point in php

Note: The value of

$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)

# The settype() function will change the original variable.

Recommended learning: "

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!

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