PHP take the remainder function introduction mod (x, y) and x%y_php tutorial

Source: Internet
Author: User
Take the remainder function PHP to take the remainder function PHP two to take the remainder MOD (x, y) x%y

MOD

For example: 9/3,9 is the divisor, 3 is the divisor. The MoD function is a redundancy function in the form of:
MoD (NEXP1,NEXP2), which is the remainder of two numeric expressions after the division operation. So: The two integers are the same as you know two positive balance (that is, two negative integers and two positive integers).

One, two odd number of integers to seek remainder

1. Function value symbol law (symbol of remainder)
MoD (negative, positive) = positive
MoD (positive, negative) = negative
Conclusion: When the remainder of two integers is obtained, the symbol of its value is the divisor symbol.
2. Rule of value
Consider two integers as positive, then divide
When the ① can be divisible, the value is 0.
When ② cannot be divisible, its value = divisor x (whole quotient +1)-dividend
Example: MoD (9,-8) =-7
That is: 9 divided by 8 of the integer quotient is 1, plus 1 is 2, and the product of the divisor is 18, and the difference between the number of 7, take the divisor of the symbol. So the value is-7.

second, two decimals to seek the remainder

Rule of value: the divisor-(quotient x divisor) is rounded after the first decimal digit.
Example: MoD (9,1.2) =1
namely: 9 except 1.2 its whole quotient for 7;7 and divisor 1.2 of the product for 8.4;8.4 rounding is 8; the difference between 9 and 8 is 1. Therefore, the result is 1.
Example: MoD (9,2.4) =0
namely: 9 except 2.2 Its whole quotient is 4;4 with divisor 2.2 This product for 8.8;8.8 rounding is 9; the difference between 9 and 9 is 0. Therefore, the result is 0.
OK, I don't know if I can meet your needs?
%

Copy the Code code as follows:
Echo 15%4;
The result is 3.


Say something about the PHP fetch operation (%)

Today I see Baidu know this example: http://zhidao.baidu.com/question/41130500.html, just want to study the next PHP to take the remainder!

Copy the Code code as follows:
$val = 9.45;
$result = $val *100;
echo $result% 100; 44
?>

Questioner wondered why the above output of the demerit is 44 instead of the imagined 45, if you do:

Echo 945%100//45

The result should be 45, because PHP defaults to rounding the variables for the remainder operation.

About PHP floating-point numbers, Situation laruence blog post: http://www.laruence.com/2013/03/26/2884.html

Then look at the following PHP code:

Copy the Code code as follows:
/*
* Name:mckee
* blog:http://www.phpddt.com
*/
$val = 9.45;
$result = $val *100;
echo intval ($result); Output here 944
echo $result% 100; Output here 44
Echo Fmod (Floatval ($result), 100); Output here 45
?>

For large integers, PHP overflows and may return a negative number (this is because PHP defaults to using integer redundancy, so you have to convert it to a float type (above)) to do this:

Copy the Code code as follows:
function Kmod ($bn, $SN)
{
Return Intval (Fmod (Floatval ($bn), $SN));
}

http://www.bkjia.com/PHPjc/770579.html www.bkjia.com true http://www.bkjia.com/PHPjc/770579.html techarticle The remainder function PHP takes the remainder function php two take away mod (x, y) x%y mod For example: 9/3,9 is the divisor, 3 is the divisor. The MoD function is a function of finding redundancy, in the form of: MoD (NEXP1,NEXP2), which is two ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.