Ruby
Rubyで絶対値を取得する
対象となる数値の絶対値を取得します。整数の場合も小数の場合も、Numericクラスのabsメソッドを使用します。
get_absolute_value.rb
# Get absolute value of integer print("Absolute value of 5 is ", 5.abs, "\n") print("Absolute value of -5 is ", (-5).abs, "\n") # Get absolute value of float print("Absolute value of 5.5 is ", 5.5.abs, "\n") print("Absolute value of -5.5 is ", (-5.5).abs, "\n")
実行結果
$ ruby get_absolute_value.rb 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