PHP floating point usage and problem summary

Source: Internet
Author: User
There are many floating point number processing functions in php, including ceil, floor, round, and intval. after they are processed, they can return the expected floating point number decimal places. I will introduce them below.

There are many floating point number processing functions in php, including ceil, floor, round, and intval. after they are processed, they can return the expected floating point decimal places, next I will introduce some usage and problem analysis of php floating point numbers.

I. php floating point usage

PHP floating point type integer ceil-one method integer

Note:Float ceil (float value)

Returns the next integer of no less than value. if the value has a decimal part, it is entered in one place. The type returned by ceil () is still float, because the float value range is usually larger than integer.

Example 1. ceil () example, instanceThe code is as follows:

  1. Echo ceil (4.3); // 5
  2. Echo ceil (9.999); // 10
  3. ?>

Floor-rounding method for integer of PHP floating point type

Note:Float floor (float value)

Returns the next integer not greater than value and rounds the decimal part of value. The return type of floor () is still float, because the float value range is usually larger than that of integer.

Example 1. floor () example,The code is as follows:

  1. Echo floor (4.3); // 4
  2. Echo floor (9.999); // 9
  3. ?>

Round of PHP floating point type-round of floating point number

Note:Float round (float val [, int precision])

Returns the result of rounding val by the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).

Example 1. round () example,The code is as follows:

  1. Echo round (3.4); // 3
  2. Echo round (3.5); // 4
  3. Echo round (3.6); // 4
  4. Echo round (3.6, 0); // 4
  5. Echo round (1.95583, 2); // 1.96
  6. Echo round (1241757,-3); // 1242000
  7. Echo round (5.045, 2); // 5.05
  8. Echo round (5.055, 2); // 5.06
  9. ?>

PHP floating point type integer intval-convert variable to integer type

Example intval (),The code is as follows:

  1. Echo intval (4.3); // 4
  2. Echo intval (4.6); // 4
  3. ?>

2. convert a floating point to a string

PHP built-in echo, var_dump, json_encode, string concatenation and other functions (commands) have problems when displaying floating point numbers, resulting in loss of precision. the instance code is as follows:

  1. $ A = 1315537636.338467;
  2. Printf ("% f", $ a); echo "n ";
  3. Echo $ a. "n ";
  4. Echo $ a; echo "n ";
  5. ?>
  6. /*
  7. Result
  8. 1315537636.338467
  9. 1315537636.3385
  10. 1315537636.3385
  11. */

That is to say, it is impossible to convert a floating point number to a string or display it in PHP's best way. you must use printf/sprintf to convert a floating point number to a string.

3. answers to a common question

However, I missed a point at the time, that is, to answer the following Frequently Asked Questions, the instance code is as follows:

  1. $ F = 0.58;
  2. Var_dump (intval ($ f* 100 ));
  3. ?>

Why output 57

Why is output 57? PHP bug? I believe many people have such questions, because there are many people who ask similar questions, not to mention bugs.php.net...

To understand this, first we need to know the floating point representation (IEEE 754 ):

Floating point number. taking the 64-bit length (double precision) as an example, one-bit sign bit (E), 11 index bit (Q), and 52-bit ending number (M) are used) (64-bit in total ).

Symbol bit:The highest bit indicates positive and negative data, 0 indicates positive data, and 1 indicates negative data.

Index bit:Indicates the power of data at the bottom of 2, and the exponent is represented by an offset code.

Ending number:Indicates the valid number after the decimal point of the data.

The key point here is that decimal places are represented in binary. you can refer to Baidu for how decimal places are represented in binary. I will not describe them here. we need to understand the key points, 0.58 for binary representation, it is an infinitely long value (the following number saves the implicit 1 )..

The binary representation of 0.58 is basically (52 bits:

0010100011110101110000101000111101011100001010001111

The binary representation of 0.57 is basically (52 bits:

0010001111010111000010100011110101110000101000111101

The binary values of the two are:

0.58-> 0.57999999999999996

0.57-> 0.56999999999999995

As for the specific Floating-point multiplication of 0.58*100, we will not consider that detail. if you are interested, you can see the Floating point. we will look at it in a fuzzy way... 0.58*100 = 57.999999999

Then you intval It. Naturally, it's 57 ....

It can be seen that the key point of this problem is: "You seem to have a poor decimal number, but it is infinite in the binary representation of the computer"

4. float (floating point number) comparison

Recently, when I was developing a contract management system, two floating point numbers were involved, which made me miserable.

A long time ago, N did not know where to hear a "truth" that "do not use equal signs to compare floating-point numbers". he was using it at ordinary times. It seemed that there was no problem, but this problem is finally solved. the instance code is as follows:

  1. $ Sum = "12300.00 ";
  2. $ A = "10000.30 ";
  3. $ B = "2000.30 ";
  4. $ C = "299.40 ";
  5. $ Sum = (float) $ sum;
  6. $ S = (float) ($ a + $ B + $ c );
  7. Var_dump ($ sum, $ s );
  8. Var_dump ($ sum = $ s );
  9. /*
  10. The result is:
  11. Float (12300)
  12. Float (12300)
  13. Bool (false)
  14. */

Later I learned that in PHP, to compare the two floating point numbers, we can use bccomp (parameter 1, parameter 2, decimal place) to compare them. The instance code is as follows:

  1. $ Sum = "12300.00 ";
  2. $ A = "10000.30 ";
  3. $ B = "2000.30 ";
  4. $ C = "299.40 ";
  5. $ Sum = (float) $ sum;
  6. $ S = (float) ($ a + $ B + $ c );
  7. Var_dump ($ sum, $ s );
  8. Var_dump (bccomp ($ sum, $ s, 2 ));
  9. /*
  10. Result:
  11. Float (12300)
  12. Float (12300)
  13. Int (0) // 0 indicates that two floating point values are equal
  14. */

Use the bccomp function in the PHP Manual.

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.