Home >Backend Development >PHP Tutorial >PHP: Detailed explanation of floating point (float) data type examples

PHP: Detailed explanation of floating point (float) data type examples

怪我咯
怪我咯Original
2017-04-27 17:04:3813697browse

What is the floating point (float) data type?

Floating point data types are our common decimals. Think "0.7" and "100.2", these are all floating point data. Floating point data types can be used to store both integers and decimals. . It has higher precision than the integer data type we talked about earlier.

The valid range of floating point type is 1.8E-308~1.8E+308.

Before PHP4.0, floating-point data was identified as double, also called double-precision floating-point number. There was no difference between the two.

There are two writing formats for floating-point data by default,A standard format, as follows

3.1415
-35.8

There is also a scientific notation format

3.14*10^3 可以使用3.14e3来表示

Float data type example

In the example below, we will test different numbers. PHP var_dump() will return the data type and value of the variable:

<?php

$x = 10.3605;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>

Code running result:

PHP: Detailed explanation of floating point (float) data type examples

PHP: Detailed explanation of floating point (float) data type examplesFloating point data is only an approximation value, so try to avoid comparisons between floating point values, because the final results are inaccurate. The result of running the code like the following

<?php

$a=0.1;
$b=0.7;
if(($a+$b)==0.8){
    echo "true";
}else{
    echo &#39;false&#39;;
}
?>

:

PHP: Detailed explanation of floating point (float) data type examples


We talked about the four scalar data types in PHP , boolean,string(string),integer(integer), plus the floating point type (float) in this chapter. In the next section, we will talk about "array (array)" among the two data types in PHP.

The above is the detailed content of PHP: Detailed explanation of floating point (float) data type examples. 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