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

怪我咯
Release: 2023-03-07 15:44:02
Original
13654 people have browsed it

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
Copy after login

There is also a scientific notation format

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

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);
?>
Copy after login

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;;
}
?>
Copy after login

:

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template