Home >Backend Development >PHP Problem >How to implement division and remainder in PHP

How to implement division and remainder in PHP

藏色散人
藏色散人Original
2020-09-04 10:34:436757browse

How to implement PHP division to get the remainder: first create a PHP sample file; then get the remainder through the modulo operator in PHP, the statement is "$x % $y"; finally output the remainder through echo .

How to implement division and remainder in PHP

Recommended: "PHP Video Tutorial"

Let’s first look at the explanation of PHP’s arithmetic operators Picture:

In PHP operations, taking the quotient (remainder) of two numbers is very simple. You can get it by using the module:

<?php 
$x=5; 
$y=3;
 
echo ($x % $y); // 输出2
echo &#39;<br>';  // 换行
 
?>

But the number obtained in the division operation is sometimes not the number we want. For example,

<?php 
$x=10; 
$y=6;

echo ($x / $y); // 输出1.6666666666667
echo &#39;<br>';  // 换行

?>

Sometimes we only want the number "1", but the simple division operation cannot get it;

PHP7 version has a new integer division operator intp(), usage example:

<?php
var_dump(intp(10, 3));
?>

The output of the above example is

int(3)

Solution in lower versions of PHP

1. ceil - rounding function
Detailed explanation of function
float ceil (float value)
Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.
Example:

ceil(4.33) 5

2. floor - rounding function

Detailed explanation of function
float floor (float value)
Returns the next integer not greater than value, and rounds the decimal part of value. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.
Example:

echo floor(9.99) 9

3. round — rounding function

Detailed explanation of function
float round (float val [, int precision] )
Returns the result of rounding val according to the specified precision precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).
Example 1. round() Example

round(3.4); 3

round(3.5); 4

Because we take The data types returned by the integer function are all float types. Generally, we can use the intval function to force the data type to an integer type.

Example:
intval—Force data into integer type

intval(4.3) 4

intval(4.7) 4

or above Just some solutions to deal with business problems. I hope they can help everyone!

The above is the detailed content of How to implement division and remainder 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