Home  >  Article  >  Backend Development  >  Summary of methods and examples of taking integers in PHP

Summary of methods and examples of taking integers in PHP

WBOY
WBOYOriginal
2016-07-25 08:58:501180browse
  1. $a=20;
  2. $b = 6;
  3. echo ($a/$b)."
    "; //out 3.3333333333333
  4. echo ceil($a/$ b)."
    "; //out 4
  5. echo floor($a/$b)."
    "; //out 3
  6. echo round($a/$b)."< br>"; //out 3
  7. //by bbs.it-home.org
  8. ?>
Copy code

In addition, I will introduce to you the four commonly used methods of taking integers in PHP.

Four functions are mainly used: ceil, floor, round, intval.

1, ceil — rounding to the next round illustrate floatceil(floatvalue) Returns the next integer that is not less than value. If value has a decimal part, it is rounded up. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer. example:

  1. echo ceil(4.3); // 5
  2. echo ceil(9.999); // 10
  3. ?>
Copy code

2, floor — rounding method all illustrate float floor (float value) Returns the next integer not greater than value, with the decimal part of value rounded off. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer. example:

  1. echo floor(4.3); // 4
  2. echo floor(9.999); // 9
  3. ?>
Copy code

3, round — perform a floating point number rounding illustrate float round ( float val [, int precision] ) Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default). example:

  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. ?>
Copy code

4, intval — Convert variables to integer type example:

  1. echo intval(4.3); //4
  2. echo intval(4.6); // 4
  3. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn