Home >Backend Development >PHP Tutorial >PHP Mathematical Function Practice 2: All-round Application of Round() Function
In PHP Mathematical Function Practice 1, I introduced to you the use of max and min functions in commonly used mathematical functions. Interested friends please click here ->《max() and min() functions Interesting analysis》. As mentioned above, I will write a series of articles on mathematical functions, isn’t this coming!
In this article, we will explain to you the use of the round function. I believe everyone is familiar with the calculation of rounding. After all, this is the basic content of nine-year compulsory teaching, so the function of this function is to Numbers are rounded.
Old rules, we assume there is such a problem that needs to be solved, "Please write a PHP script to round the following values with 1 decimal digit precision".
Simply put, it is to round the specified value and retain one decimal place.
Without further ado, we also create a PHP sample file demo.php;
##The complete code is as follows:<?php echo round( 1.65, 1, PHP_ROUND_HALF_UP)."<br>"; // 1.7 echo round( 1.65, 1, PHP_ROUND_HALF_DOWN)."<br>"; // 1.6 echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5The above code , parameter 1.65 is a specified number that needs to be rounded, parameter 1 is the number of digits to retain, and parameter PHP_ROUND_HALF_UP represents the constant of the rounding mode, which means rounding up when encountering .5. Let’s open the browser to take a look and output the rounding result: We can see that the rounding result is different, because for each The constant parameters set by the number are different. For details, please see the following constant meaning introduction: PHP_ROUND_HALF_UP: When encountering .5, the number will be rounded up to precision decimal places. Rounds 1.5 to 2 and -1.5 to -2. PHP_ROUND_HALF_DOWN: Round number down to precision decimal places when encountering .5. Rounds 1.5 to 1 and -1.5 to -1. PHP_ROUND_HALF_EVEN: When encountering .5, take the next even value and round number to precision decimal places. PHP_ROUND_HALF_ODD: When encountering .5, take the next odd value and round number to precision decimal places. So if we do not set this constant parameter and use the round function directly, which is the default constant? The default is "PHP_ROUND_HALF_UP". The above is the introduction to the round function. Is it very simple? In the actual development process, you can use the round function according to specific needs! For more PHP technical teaching, you can visit
PHP video tutorial!
The above is the detailed content of PHP Mathematical Function Practice 2: All-round Application of Round() Function. For more information, please follow other related articles on the PHP Chinese website!