Export (0) Print
Expand All

Math.DivRem Method (Int64, Int64, Int64%)

Updated: February 2009

Calculates the quotient of two 64-bit signed integers and also returns the remainder in an output parameter.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static long DivRem(
	long a,
	long b,
	out long result
)

Parameters

a
Type: System.Int64

The dividend.

b
Type: System.Int64

The divisor.

result
Type: System.Int64%

The remainder.

Return Value

Type: System.Int64
The quotient of the specified numbers.

ExceptionCondition
DivideByZeroException

b is zero.

The remainder is calculated by using the modulus operation.

The following example demonstrates the Math.DivRem(Int64, Int64, Int64) method.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1

Date

History

Reason

February 2009

Added the Remarks section and replaced the example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft