📜  C#| Math.Ceiling()方法

📅  最后修改于: 2021-05-29 16:15:29             🧑  作者: Mango

在C#中, Math.Ceiling()是Math类的方法。此方法用于查找最小整数,该整数大于或等于传递的参数。 Celing方法同时使用十进制和双精度两种功能。通过向其传递不同的参数,可以使此方法过载。

  • Math.Ceiling(Decimal)方法
  • Math.Ceiling(Double)方法

Math.Ceiling(Decimal)方法

此方法用于返回大于或等于参数列表中指定的十进制数字的最小整数值

句法:

public static decimal Ceiling(decimal d)

范围:

返回类型:该函数返回最小的整数值,该整数值将大于或等于d。此方法的类型为System.Decimal,并返回小数而不是整数类型。

例子:

Input  : 888.765M;
Output : 889

Input  : -20002.999M
Output : -20002

程序:演示Math.Ceiling(Decimal)方法。

// C# program to illustrate the
// Math.Ceiling(Decimal) function
using System;
  
class SudoPlacement {
  
    // Main method
    static void Main()
    {
  
        // Input decimal value.
        decimal decim_n1 = 2.10M;
        decimal decim_n2 = -99.90M;
        decimal decim_n3 = 33.001M;
  
        // Calculate Ceiling values by
        // Using Math.Ceiling() function
        decimal ceil_t1 = Math.Ceiling(decim_n1);
        decimal ceil_t2 = Math.Ceiling(decim_n2);
        decimal ceil_t3 = Math.Ceiling(decim_n3);
  
        // Print First values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n1);
        Console.WriteLine("Ceiling value = " + ceil_t1);
  
        // Print Second values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n2);
        Console.WriteLine("Ceiling value = " + ceil_t2);
  
        // Print third values and Ceiling
        Console.WriteLine("Input Value  = " + decim_n3);
        Console.WriteLine("Ceiling value = " + ceil_t3);
    }
}
输出:
Input Value  = 2.10
Ceiling value = 3
Input Value  = -99.90
Ceiling value = -99
Input Value  = 33.001
Ceiling value = 34

Math.Ceiling(Double)方法

此方法用于返回大于或等于参数列表中指定的双精度浮点数的最小整数值。

句法:

public static double Ceiling(double d)

范围:

返回类型:此方法返回大于或等于d的最小整数值。如果d等于NaN,NegativeInfinity或PositiveInfinity,则返回该值。此方法的类型为System.Double

例子:

Input  : 10.1  
Output : 11

Input  : -2222.2220
Output : -2222

程序:演示Math.Ceiling(Double)方法。

// C# program to illustrate the
// Math.Ceiling(Double) function
using System;
  
class SudoPlacement {
  
    // Main method
    static void Main()
    {
  
        // Input different Double value.
        double n1 = 101.10;
        double n2 = -1.1;
        double n3 = 9222.1000;
  
        // Calculate Ceiling values by
        // Using Math.Ceiling() function
        double t1 = Math.Ceiling(n1);
        double t2 = Math.Ceiling(n2);
        double t3 = Math.Ceiling(n3);
  
        // Print First values and Ceiling
        Console.WriteLine("Input Value  = " + n1);
        Console.WriteLine("Ceiling value = " + t1);
  
        // Print Second values and Ceiling
        Console.WriteLine("Input Value  = " + n2);
        Console.WriteLine("Ceiling value = " + t2);
  
        // Print third values and Ceiling
        Console.WriteLine("Input Value  = " + n3);
        Console.WriteLine("Ceiling value = " + t3);
    }
}
输出:
Input Value  = 101.1
Ceiling value = 102
Input Value  = -1.1
Ceiling value = -1
Input Value  = 9222.1
Ceiling value = 9223

参考:

  • https://msdn.microsoft.com/zh-CN/library/1cz5da1c(v=vs.110).aspx
  • https://msdn.microsoft.com/zh-CN/library/zx4t0t48(v=vs.110).aspx