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

Apr 27, 2017 pm 05:04 PM

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

What is the best data type for gender fields in MySQL?

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

What data type should be used for gender field in MySQL database?

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Mind map of Python syntax: in-depth understanding of code structure

Python entry to proficiency: from zero basics to project development Python entry to proficiency: from zero basics to project development Feb 20, 2024 am 11:42 AM

Python entry to proficiency: from zero basics to project development

What are the python data types? What are the python data types? Dec 11, 2023 pm 04:17 PM

What are the python data types?

Detailed explanation of how to use Boolean type in MySQL Detailed explanation of how to use Boolean type in MySQL Mar 15, 2024 am 11:45 AM

Detailed explanation of how to use Boolean type in MySQL

What is the best data type choice for gender field in MySQL? What is the best data type choice for gender field in MySQL? Mar 14, 2024 pm 01:24 PM

What is the best data type choice for gender field in MySQL?

What are the data types of java What are the data types of java Jan 30, 2024 pm 03:23 PM

What are the data types of java

See all articles