Math.DivRem 方法

定义

计算两个数字的商,并返回输出参数中的余数。

重载

DivRem(Int64, Int64, Int64)

计算两个 64 位有符号整数的商,并返回输出参数中的余数。

DivRem(Int32, Int32, Int32)

计算两个 32 位有符号整数的商,并返回输出参数中的余数。

DivRem(UIntPtr, UIntPtr)

生成两个无符号本机大小数字的商和余数。

DivRem(UInt64, UInt64)

生成两个无符号 64 位数字的商和余数。

DivRem(UInt32, UInt32)

生成两个无符号 32 位数字的商和余数。

DivRem(UInt16, UInt16)

生成两个无符号 16 位数字的商和余数。

DivRem(SByte, SByte)

生成两个有符号 8 位数字的商和余数。

DivRem(Int64, Int64)

生成两个有符号 64 位数字的商和其余部分。

DivRem(Int32, Int32)

生成两个有符号 32 位数字的商和其余部分。

DivRem(Int16, Int16)

生成两个有符号 16 位数字的商和余数。

DivRem(Byte, Byte)

生成两个无符号 8 位数字的商和余数。

DivRem(IntPtr, IntPtr)

生成两个有符号本机大小数字的商和其余部分。

DivRem(Int64, Int64, Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

计算两个 64 位有符号整数的商,并返回输出参数中的余数。

C#
public static long DivRem (long a, long b, out long result);

参数

a
Int64

股息。

b
Int64

除数。

result
Int64

此方法返回时,包含余数。

返回

指定数字的商。

例外

b 为零。

示例

以下示例演示 DivRem(Int64, Int64, Int64) 方法。

C#
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

注解

余数值等于 余数运算符的结果

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DivRem(Int32, Int32, Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

计算两个 32 位有符号整数的商,并返回输出参数中的余数。

C#
public static int DivRem (int a, int b, out int result);

参数

a
Int32

股息。

b
Int32

除数。

result
Int32

此方法返回时,包含余数。

返回

指定数字的商。

例外

b 为零。

示例

以下示例演示 DivRem(Int32, Int32, Int32) 方法。

C#
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

注解

余数值等于 余数运算符的结果

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DivRem(UIntPtr, UIntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

重要

此 API 不符合 CLS。

生成两个无符号本机大小数字的商和余数。

C#
[System.CLSCompliant(false)]
public static (nuint Quotient, nuint Remainder) DivRem (nuint left, nuint right);
C#
[System.CLSCompliant(false)]
public static (UIntPtr Quotient, UIntPtr Remainder) DivRem (UIntPtr left, UIntPtr right);

参数

left

nuint

股息。

right

nuint

除数。

返回

ValueTuple<nuint,nuint>

指定数字的商和余数。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(UInt64, UInt64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

重要

此 API 不符合 CLS。

生成两个无符号 64 位数字的商和余数。

C#
[System.CLSCompliant(false)]
public static (ulong Quotient, ulong Remainder) DivRem (ulong left, ulong right);

参数

left
UInt64

股息。

right
UInt64

除数。

返回

指定数字的商和余数。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(UInt32, UInt32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

重要

此 API 不符合 CLS。

生成两个无符号 32 位数字的商和余数。

C#
[System.CLSCompliant(false)]
public static (uint Quotient, uint Remainder) DivRem (uint left, uint right);

参数

left
UInt32

股息。

right
UInt32

除数。

返回

指定数字的商和余数。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(UInt16, UInt16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

重要

此 API 不符合 CLS。

生成两个无符号 16 位数字的商和余数。

C#
[System.CLSCompliant(false)]
public static (ushort Quotient, ushort Remainder) DivRem (ushort left, ushort right);

参数

left
UInt16

股息。

right
UInt16

除数。

返回

指定数字的商和余数。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(SByte, SByte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

重要

此 API 不符合 CLS。

生成两个有符号 8 位数字的商和余数。

C#
[System.CLSCompliant(false)]
public static (sbyte Quotient, sbyte Remainder) DivRem (sbyte left, sbyte right);

参数

left
SByte

股息。

right
SByte

除数。

返回

指定数字的商和余数。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(Int64, Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

生成两个有符号 64 位数字的商和其余部分。

C#
public static (long Quotient, long Remainder) DivRem (long left, long right);

参数

left
Int64

股息。

right
Int64

除数。

返回

指定数字的商和余数。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(Int32, Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

生成两个有符号 32 位数字的商和其余部分。

C#
public static (int Quotient, int Remainder) DivRem (int left, int right);

参数

left
Int32

股息。

right
Int32

除数。

返回

指定数字的商和余数。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(Int16, Int16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

生成两个有符号 16 位数字的商和余数。

C#
public static (short Quotient, short Remainder) DivRem (short left, short right);

参数

left
Int16

股息。

right
Int16

除数。

返回

指定数字的商和余数。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(Byte, Byte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

生成两个无符号 8 位数字的商和余数。

C#
public static (byte Quotient, byte Remainder) DivRem (byte left, byte right);

参数

left
Byte

股息。

right
Byte

除数。

返回

指定数字的商和余数。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

DivRem(IntPtr, IntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

生成两个有符号本机大小数字的商和其余部分。

C#
public static (nint Quotient, nint Remainder) DivRem (nint left, nint right);
C#
public static (IntPtr Quotient, IntPtr Remainder) DivRem (IntPtr left, IntPtr right);

参数

left

nint

股息。

right

nint

除数。

返回

ValueTuple<nint,nint>

指定数字的商和余数。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9