Export (0) Print
Expand All

Math.DivRem Method (Int64, Int64, Int64)

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)

'Declaration
Public Shared Function DivRem ( _
	a As Long, _
	b As Long, _
	<OutAttribute> ByRef result As Long _
) As Long

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 DivRem(Int64, Int64, Int64) method.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft