C#
C#で絶対値を取得する
引数で与えた数値の絶対値を取得します。整数の場合も、小数の場合も、System.MathクラスのAbs()を使用します。
GetAbsoluteValue.cs
using System; class GetAbsoluteValue { static void Main() { // Get absolute value of integer Console.WriteLine("Absolute value of 5 is " + System.Math.Abs(5)); Console.WriteLine("Absolute value of -5 is " + System.Math.Abs(-5)); // Get absolute value of decimal Console.WriteLine("Absolute value of 5.5 is " + System.Math.Abs(5.5)); Console.WriteLine("Absolute value of -5.5 is " + System.Math.Abs(-5.5)); } }
実行結果
$ mcs GetAbsoluteValue.cs $ mono GetAbsoluteValue.exe Absolute value of 5 is 5 Absolute value of -5 is 5 Absolute value of 5.5 is 5.5 Absolute value of -5.5 is 5.5