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 Systemusing System;// create classclass RemainderGetter{// main methodstatic void Main(){// create decimal valuesdecimal d1 = 35.5m;decimal d2 = 4.1m;decimal d3 = 300.34m;decimal d4 = 5.25m;// get remainderdecimal r1 = Decimal.Remainder(d1, d2);decimal r2 = Decimal.Remainder(d3, d4);// print resultsConsole.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 ofd1byd2. -
Line 18: We use the
Decimal.Remainder()method to get the remainder from the division ofd3byd4. -
Lines 21–22: We print the results to the console.