Home  >  Article  >  Backend Development  >  PHP rounding, rounding, use of round function

PHP rounding, rounding, use of round function

不言
不言Original
2018-06-06 11:21:252455browse

This article mainly introduces PHP rounding, rounding, and round function usage examples. Friends in need can refer to the

decimal examples:

PHP retains two decimal places and Rounding

Copy code The code is as follows:

$n=0.1265489;
echo sprintf("%.2f", $n); // 0.13

You can see that we used the sprintf function to format $n%.2f is the target format , where 2 represents two digits and f represents float (floating point type). The 3rd is the decimal 6 and is rounded off.
Let’s look at another example.

Copy code The code is as follows:

$n=0.1265489
echo substr(sprintf("%.3",$n),0,-1);// 0.12

The code output retains 2 as a decimal without rounding. In fact, we understand that the characteristics of sprintf will round the decimal and we retain one more digit, and then use substr to intercept the first 2 digits

Rounding Example:

Copy code The code is as follows:

echo ceil(4.1);    // 5
echo ceil(9.999);  // 10

The ceil function is an upward rounding function. What does upward mean? That is to say, if it exceeds a little bit, move forward one bit. For example, 4.1 becomes 5 in the example.

On the contrary, there is a function called floor. Let’s take a look at its usage.

Copy code The code is as follows:

echo floor(4.1);   // 4
echo floor(9.999); // 9

The characteristics of floor are in Chapter 1 The two outputs are particularly obvious, that is, without giving you how many decimal places, even if it is infinitely close to 10, the integer you get by going down is 9.

round function

Copy code The code is as follows:

<?PHP
  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
?>

The description of the round function in the PHP manual is:

float round (float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
Returns the result of rounding val according to the specified precision (the number of digits after the decimal point). precision can also be negative or zero (default).

#The first parameter of round is the data source, the second parameter is the number of decimal places to be retained and the next one (for example, if you enter 2, then the third one is the next one) is rounded, when he When it is a negative number, start from the last digit of the data source and count forward the corresponding length to 0 and round the last digit. For example, round(123456,-2) is 123456 starting from 6 and counting forward two digits become zero, and The last digit of 5 (the first digit from back to front is 6 and the last digit is 5) is rounded off, and the output is 123500

Related recommendations:

The usage and difference between intval() and (int) conversion under PHP

The above is the detailed content of PHP rounding, rounding, use of round function. For more information, please follow other related articles on the PHP Chinese website!

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