Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal.Remainder Method

Computes the remainder after dividing two Decimal values.

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

public static decimal Remainder(
	decimal d1,
	decimal d2
)

Parameters

d1
Type: System.Decimal

The dividend.

d2
Type: System.Decimal

The divisor.

Return Value

Type: System.Decimal
The remainder after dividing d1 by d2.

ExceptionCondition
DivideByZeroException

d2 is zero.

OverflowException

The return value is less than MinValue or greater than MaxValue.

The following example uses the Remainder method to calculate the remainder in a series of division operations.

using System;

public class Example
{
    public static void Main()
    {
        // Create parallel arrays of Decimals to use as the dividend and divisor.
        Decimal[] dividends = { 79m, 1000m, -1000m, 123m, 1234567800000m,
                                1234.0123m };
        Decimal[] divisors = { 11m, 7m, 7m, .00123m, 0.12345678m, 1234.5678m };

        for (int ctr = 0; ctr < dividends.Length; ctr++) 
        {
           Decimal dividend = dividends[ctr];
           Decimal divisor = divisors[ctr];
           Console.WriteLine("{0:N3} / {1:N3} = {2:N3} Remainder {3:N3}", dividend,
                             divisor, Decimal.Divide(dividend, divisor),
                             Decimal.Remainder(dividend, divisor)); 
        }
    }
} 
// The example displays the following output: 
//       79.000 / 11.000 = 7.182 Remainder 2.000 
//       1,000.000 / 7.000 = 142.857 Remainder 6.000 
//       -1,000.000 / 7.000 = -142.857 Remainder -6.000 
//       123.000 / 0.001 = 100,000.000 Remainder 0.000 
//       1,234,567,800,000.000 / 0.123 = 10,000,000,000,000.000 Remainder 0.000 
//       1,234.012 / 1,234.568 = 1.000 Remainder 1,234.012

.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

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft