What is Decimal.Remainder() in C#?

Overview

In C#, Decimal.Remainder() is used to calculate the remainder after dividing two decimal values.

Syntax

public static decimal Remainder (decimal d1, decimal d2);

Parameters

d1: This is a mandatory decimal number. It is the dividend.

d2: This is also a mandatory decimal number. It is the divisor.

Return value

A decimal value is returned, which is the remainder from the division of d1 by d2.

Code

Run the code below to see the working of the remainder() method:

// use System
using System;
// create class
class RemainderGetter
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 35.5m;
decimal d2 = 4.1m;
decimal d3 = 300.34m;
decimal d4 = 5.25m;
// get remainder
decimal r1 = Decimal.Remainder(d1, d2);
decimal r2 = Decimal.Remainder(d3, d4);
// print results
Console.WriteLine(r1);
Console.WriteLine(r2);
}
}

Explanation

  • Lines 11–14: We create several decimal numbers.

  • Line 17: We call the Decimal.Remainder() method to get the remainder from the division of d1 by d2.

  • Line 18: We use the Decimal.Remainder() method to get the remainder from the division of d3 by d4.

  • Lines 21–22: We print the results to the console.

Free Resources