Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด MathF.Floor() ใน C# พร้อมตัวอย่าง


เมธอด MathF.Floor() ใน C# ใช้สำหรับหาจำนวนเต็มที่มากที่สุดซึ่งน้อยกว่าหรือเท่ากับค่า float ที่ระบุ

ไวยากรณ์

public static float Floor (float val);

พารามิเตอร์ val คือค่าจุดทศนิยมที่ต้องการปัดลง

ตัวอย่าง

using System; 
class Demo { 
    public static void Main(){ 
        float val = 45.67f; 
        float res = MathF.Floor(val); 
        Console.WriteLine("MathF.Floor() = " + res); 
    } 
}

ผลลัพธ์

MathF.Floor() = 45

ตัวอย่างที่ 2 (ค่าลบ)

using System; 
class Demo { 
    public static void Main(){ 
        float val = -10.33f; 
        float res = MathF.Floor(val); 
        Console.WriteLine("MathF.Floor() = " + res); 
    } 
}

ผลลัพธ์

MathF.Floor() = -11