Home  >  Article  >  Backend Development  >  How to implement division and rounding of two numbers in PHP

How to implement division and rounding of two numbers in PHP

青灯夜游
青灯夜游Original
2022-01-24 11:28:336095browse

php method to realize division and rounding of two numbers: 1. Use the "/" operator to perform division operation, the syntax is "value 1 / value 2"; 2. Use the rounding functions intval(), round( ), ceil() or floor() just round the result of the division operation.

How to implement division and rounding of two numbers in PHP

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php implements the division of two numbers The whole number can be divided into two parts:

  • Dividing two numbers: You can use the "/" operator to perform division operation

  • Rounding: You can use the rounding function to round the result of the division operation

There are four commonly used methods of PHP rounding function:

  • Round directly, discard decimals, keep integers: intval();

  • Round to integers: round();

  • Round up Integer, if there are decimals, add 1: ceil();

  • Round down to integer: floor().

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$a=10;
$b=3;
$c=$a/$b;
echo "两数相除的结果:".$c;
echo "<br>使用intval()取整:".intval($c);
echo "<br>使用round()取整:".round($c);
echo "<br>使用ceil()取整:".ceil($c);
echo "<br>使用floor()取整:".floor($c);
?>

How to implement division and rounding of two numbers in PHP

##Expand knowledge: understand ceil, floor, round, intval functions

1. intval—convert the variable into an integer type

If intval is a character type, it will be automatically converted to 0.

intval(3.14159);  // 3
intval(3.64159);  // 3
intval(&#39;ruesin&#39;); //0

2. Rounding: round()

Round parameter 1 according to the precision specified by parameter 2. Parameter 2 can be negative or zero (default).

round(3.14159);      // 3
round(3.64159);      // 4
round(3.64159, 0);   // 4
round(3.64159, 2);   // 3.64
round(5.64159, 3);   // 3.642
round(364159, -2);   // 364200

3. Round up and add 1 if there is a decimal: ceil()

Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up by one.

This method is often used when we write paging classes to calculate the number of pages.

ceil(3.14159);  // 4
ceil(3.64159);  // 4

4. Round down: floor()

Returns the next integer that is not greater than value, and rounds the decimal part of value.

floor(3.14159);    // 3
floor(3.64159);    // 3

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to implement division and rounding of two numbers in PHP. 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