📜  C#| Math.Floor()方法

📅  最后修改于: 2021-05-29 19:28:01             🧑  作者: Mango

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

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

Math.Floor(Decimal)方法

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

句法:

public static decimal Floor(decimal d)

范围:

返回类型:此函数返回小于或等于d的最大整数。此方法的类型为System.Decimal

例子:

Input  : 12.9
Output : 12

Input  : -12.9
Output : -13

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

// C# program to illustrate the
// Math.Floor(Decimal) function
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Different numbers list to find
        // its floor values
        Console.WriteLine(Math.Floor(0.2018));
        Console.WriteLine(Math.Floor(123.123));
        Console.WriteLine(Math.Floor(-0.2));
        Console.WriteLine(Math.Floor(0.0));
        Console.WriteLine(Math.Floor(34.67M));
    }
}

输出:

0
123
-1
0
34

Math.Floor(Double)方法

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

句法:

public static double Floor(double d)

范围:

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

例子:

Input  : 987654321.012 
Output : 987654321

Input  : -99999
Output : -100000

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

// C# program to illustrate the
// Math.Floor(Double) function
using System;
class GFG {
  
    // Main method 
    static void Main()
    {
  
        // Two values.
        double n1 = 0.2018;
        double n2 = 123.123;
        double n3 = -2.2;
        double n4 = -123.123;
  
        // Take floors of these values.
        double floor1 = Math.Floor(n1);
        double floor2 = Math.Floor(n2);
        double floor3 = Math.Floor(n3);
        double floor4 = Math.Floor(n4);
  
        // Print First values and floor
  
        Console.WriteLine("value n1 = " + n1);
        Console.WriteLine("Floor1 values is = " + floor1);
  
        // Print 2nd values and floor
  
        Console.WriteLine("value n2 = " + n2);
        Console.WriteLine("Floor2 values is = " + floor2);
  
        // Print 3rd values and floor
  
        Console.WriteLine("value n3 = " + n3);
        Console.WriteLine("Floor3 values is = " + floor3);
  
        // Print 4th values and floor
  
        Console.WriteLine("value n4 = " + n4);
        Console.WriteLine("Floor4 values is = " + floor4);
    }
}

输出:

value n1 = 0.2018
Floor1 values is = 0
value n2 = 123.123
Floor2 values is = 123
value n3 = -2.2
Floor3 values is = -3
value n4 = -123.123
Floor4 values is = -124

参考: https : //msdn.microsoft.com/zh-cn/library/system.math.floor(v=vs.110).aspx