Php floating point value examples

Source: Internet
Author: User
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:

  1. $ Num = 0.12345600000000000;
  2. // 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
  3. Ini_set ("precision", "12 ");
  4. Echo $ num; /// 0.123456
  5. // If the precision value is not exceeded, the displayed result is 0.123456.
  6. Ini_set ("precision", "3 ");
  7. Echo $ num; /// 0.123
  8. // Exceeds the precision value, retain 3 bits
  9. Ini_set ("precision", "5 ");
  10. Echo $ num; /// 0.12346
  11. // 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:

  1. $ Num = 12.12345600000000000;
  2. // 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
  3. Ini_set ("precision", "12 ");
  4. Echo $ num; /// 12.123456
  5. // If the precision value is not exceeded, the displayed result is 12.123456.
  6. Ini_set ("precision", "3 ");
  7. Echo $ num; /// 12.1
  8. // Beyond the precision value, the number of digits in the integer part is 2, so only one decimal point is retained
  9. Ini_set ("precision", "5 ");
  10. Echo $ num; /// 12.123
  11. // 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:

  1. $ Num = 12345678.12345600000000000;
  2. // 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.
  3. Ini_set ("precision", "12 ");
  4. Echo $ num; /// 12345678.1235
  5. // If the precision value is exceeded, the displayed result is 12345678.1235.
  6. Ini_set ("precision", "3 ");
  7. Echo $ num; // 1.23E + 7
  8. // 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
  9. Ini_set ("precision", "5 ");
  10. Echo $ num; /// 12346000
  11. // 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:

  1. $ Num1 = 1331625729.687;
  2. $ Num2 = 1331625730.934;
  3. Ini_set ("precision", "8 ");
  4. Echo $ num1 .'
  5. ';
  6. Echo $ num2 .'
  7. ';
  8. $ Sub = $ num1-$ num2;
  9. Echo $ sub .'
  10. ';
  11. // The output result is:
  12. /*
  13. 1331625700
  14. 1331625700
  15. -1.247
  16. */

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:

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

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.

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.