当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Decimal.Remainder()用法及代码示例


当在两个指定的十进制值之间进行除法时,使用此方法来计算余数。

用法: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

参考:



相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 Decimal.Remainder() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。