PHP: Floating-point type (float) data type instance detailed

Source: Internet
Author: User
What is a floating-point (float) data type?

Floating-point data types are our common decimals, like "0.7", "100.2" are floating-point data, floating-point data types can be used to store integers or to store decimals. It's more accurate than the integer data type we talked about earlier.

The valid range for floating-point is 1.8e-308~1.8e+308.

Before PHP4.0, floating-point identifiers are double, also called double-precision floating-point numbers, and they are no different.

Floating-point data has two default writing formats, a standard format , as follows

3.1415-35.8

There is also a scientific notation format

3.14*10^3 can use 3.14E3 to indicate

Floating-point (float) data type example

In the example below, we will test for different numbers. PHP Var_dump () returns 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 Run Result:

Floating-point data is just an approximate value, so try to avoid the comparison size between floating-point numbers, because the final result is inaccurate. Like this.

<?php$a=0.1; $b =0.7;if (($a + $b) ==0.8) {    echo "true";} Else{    echo ' false ';}? >

The result of the code run:


We talked about four scalar data types in PHP, Boolean (Boolean), String, Integer (integer), plus floating point (float) in this section. In the next section, we'll talk about "array" in PHP, which conforms to the data type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.