The floating point value in php is used to control the output of the floating point. it can be understood as the number of digits of the output. the output results may vary depending on the precision value. note:
The floating point value in php is used to control the output of the floating point. it can be understood as the number of digits of the output. the output result may be different if the precision value is different. note: it is stored internally according to the actual value. when two floating point numbers perform arithmetic operations, the original value is used.
In the php configuration file, precision is used to set the global precision of the specified floating point number. it seems that the default setting of each release version is not the same. in the window, it is 12, in linux, the value is 14. you can also use ini_set in the program to change the global settings. for example:
Ini_set ("precision", "15 ");
I have always understood the precision as the number of decimal points reserved. is this true for floating point numbers in php? The answer is No.
A floating point is actually composed of an integer and a decimal part. the precision here is that the number of digits in the integer part plus the decimal part cannot exceed the maximum precision, then, the maximum precision value is truncated according to the rounding method. If the integer is 0, digits are not counted, and 0 at the end of the decimal part is not counted into digits. In addition, for the same number, different precision may display different forms. The following is an example.
If the integer is 0,The code is as follows:
- $ Num = 0.12345600000000000;
- // The integer part is 0, the number of digits is 0, and the 0 at the end of the decimal part is not counted into the number of digits, so the total number of digits is 6
- Ini_set ("precision", "12 ");
- Echo $ num; /// 0.123456
- // If the precision value is not exceeded, the displayed result is 0.123456.
- Ini_set ("precision", "3 ");
- Echo $ num; /// 0.123
- // Exceeds the precision value, retain 3 bits
- Ini_set ("precision", "5 ");
- Echo $ num; /// 0.12346
- // If the value exceeds the precision value, retain 5 digits. in this case, the precision value is equal to the number of digits after the decimal point.
If the integer is greater than 0,The code is as follows:
- $ Num = 12.12345600000000000;
- // The integer part is 12, the number of digits is 2, and the 0 at the end of the decimal part is not counted into the number of digits. the number of digits is 6, so the total number of digits is 2 + 6
- Ini_set ("precision", "12 ");
- Echo $ num; /// 12.123456
- // If the precision value is not exceeded, the displayed result is 12.123456.
- Ini_set ("precision", "3 ");
- Echo $ num; /// 12.1
- // Beyond the precision value, the number of digits in the integer part is 2, so only one decimal point is retained
- Ini_set ("precision", "5 ");
- Echo $ num; /// 12.123
- // If the precision is exceeded, the number of digits in the integer part is 2. Therefore, only three decimal places are reserved. the number of digits after the decimal point is determined by the number of digits in the precision part.
Case 2 where the integer part is greater than 0,The code is as follows:
- $ Num = 12345678.12345600000000000;
- // The integer part is 12345678, and the number of digits is 8. the value 0 at the end of the decimal part is not counted as the number of digits. the number of digits is 6, so the total number of digits is 8 + 6.
- Ini_set ("precision", "12 ");
- Echo $ num; /// 12345678.1235
- // If the precision value is exceeded, the displayed result is 12345678.1235.
- Ini_set ("precision", "3 ");
- Echo $ num; // 1.23E + 7
- // Beyond the precision value, and the number of digits in the integer part exceeds the precision, the fractional part is discarded, and the integer part is only three digits
- Ini_set ("precision", "5 ");
- Echo $ num; /// 12346000
- // Beyond the precision value, and the number of digits in the integer part exceeds the precision, the fractional part is discarded, and the integer part is only five digits
In the above example, we can see that the precision value is also related to the intercept of the integer part. Note that the methods shown in the last two examples are different. one is to use scientific notation, and the other is to use 0 complement, the experiment shows that when the number of digits in the integer part minus the precision value is greater than 4, scientific notation is used. otherwise, 0 is used to supplement the number. In other words, that is, after the number of digits in the integer part exceeds the precision value, after truncation, the number of digits supplemented by 0 will not exceed 4.
Floating point calculation,The code is as follows:
- $ Num1 = 1331625729.687;
- $ Num2 = 1331625730.934;
- Ini_set ("precision", "8 ");
- Echo $ num1 .'
- ';
- Echo $ num2 .'
- ';
- $ Sub = $ num1-$ num2;
- Echo $ sub .'
- ';
- // The output result is:
- /*
- 1331625700
- 1331625700
- -1.247
- */
The preceding example shows that the precision value only controls the display result, and the internal storage is still the original value. Therefore, the value of $ sub is 1331625729.687 minus 1331625730.934.
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.