# Math.DivRem 方法

## 重载

 DivRem(Int32, Int32, Int32) 计算两个 32 位有符号整数的商，并通过输出参数返回余数。Calculates the quotient of two 32-bit signed integers and also returns the remainder in an output parameter. DivRem(Int64, Int64, Int64) 计算两个 64 位有符号整数的商，并通过输出参数返回余数。Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.

## DivRem(Int32, Int32, Int32)

``````public:
static int DivRem(int a, int b, [Runtime::InteropServices::Out] int % result);``````
``public static int DivRem (int a, int b, out int result);``
``static member DivRem : int * int * int -> int``
``Public Shared Function DivRem (a As Integer, b As Integer, ByRef result As Integer) As Integer``

a
Int32

b
Int32

result
Int32

Int32

#### 例外

`b` 为零。`b` is zero.

### 示例

``````using System;

public class Example
{
public static void Main()
{
// Define several positive and negative dividends.
int[] dividends = { Int32.MaxValue, 13952, 0, -14032,
Int32.MinValue };
// Define one positive and one negative divisor.
int[] divisors = { 2000, -2000 };

foreach (int divisor in divisors)
{
foreach (int dividend in dividends)
{
int remainder;
int quotient = Math.DivRem(dividend, divisor, out remainder);
Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
dividend, divisor, quotient, remainder);
}
}
}
}
// The example displays the following output:
//       2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647
//       13,952 \ 2,000 = 6, remainder 1,952
//       0 \ 2,000 = 0, remainder 0
//       -14,032 \ 2,000 = -7, remainder -32
//       -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648
//       2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647
//       13,952 \ -2,000 = -6, remainder 1,952
//       0 \ -2,000 = 0, remainder 0
//       -14,032 \ -2,000 = 7, remainder -32
//       -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648
``````
``````Module Example
Public Sub Main()
' Define several positive and negative dividends.
Dim dividends() As Integer = { Int32.MaxValue, 13952, 0, -14032, _
Int32.MinValue }
' Define one positive and one negative divisor.
Dim divisors() As Integer = { 2000, -2000 }

For Each divisor As Integer In divisors
For Each dividend As Integer In dividends
Dim remainder As Integer
Dim quotient As Integer = Math.DivRem(dividend, divisor, remainder)
Console.WriteLine("{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}", _
dividend, divisor, quotient, remainder)
Next
Next
End Sub
End Module
' The example displays the following output:
'       2,147,483,647 \ 2,000 = 1,073,741, remainder 1,647
'       13,952 \ 2,000 = 6, remainder 1,952
'       0 \ 2,000 = 0, remainder 0
'       -14,032 \ 2,000 = -7, remainder -32
'       -2,147,483,648 \ 2,000 = -1,073,741, remainder -1,648
'       2,147,483,647 \ -2,000 = -1,073,741, remainder 1,647
'       13,952 \ -2,000 = -6, remainder 1,952
'       0 \ -2,000 = 0, remainder 0
'       -14,032 \ -2,000 = 7, remainder -32
'       -2,147,483,648 \ -2,000 = 1,073,741, remainder -1,648
``````

## DivRem(Int64, Int64, Int64)

``````public:
static long DivRem(long a, long b, [Runtime::InteropServices::Out] long % result);``````
``public static long DivRem (long a, long b, out long result);``
``static member DivRem : int64 * int64 * int64 -> int64``
``Public Shared Function DivRem (a As Long, b As Long, ByRef result As Long) As Long``

a
Int64

b
Int64

result
Int64

Int64

#### 例外

`b` 为零。`b` is zero.

### 示例

``````using System;

public class Example
{
public static void Main()
{
// Define several positive and negative dividends.
long[] dividends = { Int64.MaxValue, 13952, 0, -14032,
Int64.MinValue };
// Define one positive and one negative divisor.
long[] divisors = { 2000, -2000 };

foreach (long divisor in divisors)
{
foreach (long dividend in dividends)
{
long remainder;
long quotient = Math.DivRem(dividend, divisor, out remainder);
Console.WriteLine(@"{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}",
dividend, divisor, quotient, remainder);
}
}
}
}
// The example displays the following output:
//    9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807
//    13,952 \ 2,000 = 6, remainder 1,952
//    0 \ 2,000 = 0, remainder 0
//    -14,032 \ 2,000 = -7, remainder -32
//    -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808
//    9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807
//    13,952 \ -2,000 = -6, remainder 1,952
//    0 \ -2,000 = 0, remainder 0
//    -14,032 \ -2,000 = 7, remainder -32
//    -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808
``````
``````Module Example
Public Sub Main()
' Define several positive and negative dividends.
Dim dividends() As Long = { Int64.MaxValue, 13952, 0, -14032, _
Int64.MinValue }
' Define one positive and one negative divisor.
Dim divisors() As Long = { 2000, -2000 }

For Each divisor As Long In divisors
For Each dividend As Long In dividends
Dim remainder As Long
Dim quotient As Long = Math.DivRem(dividend, divisor, remainder)
Console.WriteLine("{0:N0} \ {1:N0} = {2:N0}, remainder {3:N0}", _
dividend, divisor, quotient, remainder)
Next
Next
End Sub
End Module
' The example displays the following output:
'    9,223,372,036,854,775,807 \ 2,000 = 4,611,686,018,427,387, remainder 1,807
'    13,952 \ 2,000 = 6, remainder 1,952
'    0 \ 2,000 = 0, remainder 0
'    -14,032 \ 2,000 = -7, remainder -32
'    -9,223,372,036,854,775,808 \ 2,000 = -4,611,686,018,427,387, remainder -1,808
'    9,223,372,036,854,775,807 \ -2,000 = -4,611,686,018,427,387, remainder 1,807
'    13,952 \ -2,000 = -6, remainder 1,952
'    0 \ -2,000 = 0, remainder 0
'    -14,032 \ -2,000 = 7, remainder -32
'    -9,223,372,036,854,775,808 \ -2,000 = 4,611,686,018,427,387, remainder -1,808
``````