當在兩個指定的十進製值之間進行除法時,使用此方法來計算餘數。
用法:public static decimal Remainder (decimal a1, decimal a2);
參數:
a1:此參數指定股息。
a2:此參數指定除數。
返回值:返回a1除以a2後的餘數。
異常:
- DivideByZeroException:當a2為零時,會發生這種情況。
- OverflowException:如果返回值小於MinValue或大於MaxValue。
以下示例程序旨在說明Decimal.Remainder(Decimal,Decimal)方法的用法:
示例1:
// C# program to demonstrate the
// Decimal.Remainder(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = 4.02m;
Decimal a2 = 1.11m;
// dividing and getting the remainder
// of the two Decimal value
// using Remainder() method;
Decimal value = Decimal.Remainder(a1, a2);
// Display the remainder
Console.WriteLine("Remainder is : {0}", value);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Remainder is : 0.69
示例2:對於OverflowException
// C# program to demonstrate the
// Decimal.Remainder(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = Decimal.MaxValue;
Decimal a2 = 0.02m;
// dividing and getting the remainder
// of the two Decimal value
// using Remainder() method;
Decimal value = Decimal.Remainder(a1, a2);
// Display the remainder
Console.WriteLine("Remainder is : {0}", value);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Exception Thrown: System.OverflowException
示例3:DivideByZeroException的程序
// C# program to demonstrate the
// Decimal.Remainder(Decimal,
// Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring the decimal variables
Decimal a1 = 4.02m;
Decimal a2 = 0.00m;
// dividing and getting the remainder
// of the two Decimal value
// using Remainder() method;
Decimal value = Decimal.Remainder(a1, a2);
// Display the remainder
Console.WriteLine("Remainder is : {0}", value);
}
catch (DivideByZeroException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Exception Thrown: System.DivideByZeroException
參考:
相關用法
- C# Uri.GetHashCode()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# TimeSpan.Add()用法及代碼示例
- C# Uri.IsWellFormedOriginalString()用法及代碼示例
- C# DateTime.Add()用法及代碼示例
- C# Uri.IsBaseOf(Uri)用法及代碼示例
- C# Uri.IsHexDigit()用法及代碼示例
- C# Random.Next()用法及代碼示例
- C# Uri.ToString()用法及代碼示例
- C# Uri.FromHex()用法及代碼示例
注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 Decimal.Remainder() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。