閱讀英文

共用方式為


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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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)

來源:
Math.cs
來源:
Math.cs
來源:
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