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;
<?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:
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!