當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# MathF.Abs()用法及代碼示例


MathF.Abs(Single)方法用於返回指定浮點數的絕對值。

用法: public static float Abs (float x);
Here, it takes a standard floating point number.

返回值:此方法返回的浮點值小於Single.MaxValue。


示例1:

// C# program to demonstrate the 
// MathF.Abs(Single)  Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring and initializing value 
        float value = 4.5f; 
  
        // Getting absolute float 
        // using Abs() method 
        float round = MathF.Abs(value); 
  
        // Display the value 
        Console.WriteLine("Absolute float value is {0}", 
                                                 round); 
    } 
}
輸出:
Absolute float value is 4.5

示例2:

// C# program to demonstrate the 
// MathF.Abs(Single) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // calling get() method 
        get(20f); 
        get(30.5f); 
        get(40.5f); 
        get(4294.586f); 
    } 
  
    // defining get() method 
    public static void get(float value) 
    { 
  
        // getting absolute float 
        // using Abs() method 
        float round = MathF.Abs(value); 
  
        // Display the value 
        Console.WriteLine("Float value is {0}", round); 
    } 
}
輸出:
Float value is 20
Float value is 30.5
Float value is 40.5
Float value is 4294.586


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 MathF.Abs() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。