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:
-
- Echo ceil (4.3); // 5
- Echo ceil (9.999); // 10
- ?>
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:
-
- Echo floor (4.3); // 4
- Echo floor (9.999); // 9
- ?>
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:
-
- Echo round (3.4); // 3
- Echo round (3.5); // 4
- Echo round (3.6); // 4
- Echo round (3.6, 0); // 4
- Echo round (1.95583, 2); // 1.96
- Echo round (1241757,-3); // 1242000
- Echo round (5.045, 2); // 5.05
- Echo round (5.055, 2); // 5.06
- ?>
PHP floating point type integer intval-convert variable to integer type
Example intval (),The code is as follows:
-
- Echo intval (4.3); // 4
- Echo intval (4.6); // 4
- ?>
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:
-
- $ A = 1315537636.338467;
- Printf ("% f", $ a); echo "n ";
- Echo $ a. "n ";
- Echo $ a; echo "n ";
- ?>
- /*
- Result
- 1315537636.338467
- 1315537636.3385
- 1315537636.3385
- */
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:
-
- $ F = 0.58;
- Var_dump (intval ($ f* 100 ));
- ?>
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:
-
- $ Sum = "12300.00 ";
- $ A = "10000.30 ";
- $ B = "2000.30 ";
- $ C = "299.40 ";
- $ Sum = (float) $ sum;
- $ S = (float) ($ a + $ B + $ c );
- Var_dump ($ sum, $ s );
- Var_dump ($ sum = $ s );
- /*
- The result is:
- Float (12300)
- Float (12300)
- Bool (false)
- */
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:
-
- $ Sum = "12300.00 ";
- $ A = "10000.30 ";
- $ B = "2000.30 ";
- $ C = "299.40 ";
- $ Sum = (float) $ sum;
- $ S = (float) ($ a + $ B + $ c );
- Var_dump ($ sum, $ s );
- Var_dump (bccomp ($ sum, $ s, 2 ));
- /*
- Result:
- Float (12300)
- Float (12300)
- Int (0) // 0 indicates that two floating point values are equal
- */
Use the bccomp function in the PHP Manual.