Ruby Float magnitude()方法及示例
Float magnitude()是一个float类的方法,它对浮点数进行处理并将其转换为绝对值。
语法: float.magnitude()
参数: 要转换为绝对值的浮点数值
返回: 所传递的浮点数值的绝对值
例子 #1 :
# Ruby program for magnitude() method
# Initializing value
a = -56.23333333
b = -10000.0
c = -(22 + 7.1)
# Printing result
puts "magnitude value of a : #{a.magnitude}\n\n"
puts "magnitude value of b : #{b.magnitude}\n\n"
puts "magnitude value of c : #{c.magnitude}\n\n"
输出:
magnitude value of a : 56.23333333
magnitude value of b : 10000.0
magnitude value of c : 29.1
例子#2 。
# Ruby program for magnitude() method
# Initializing value
a = -100.7 - 10.4
b = -100 * 2000.0
c = -(22 + 7.1) * 4
# Printing result
puts "magnitude value of a : #{a.magnitude}\n\n"
puts "magnitude value of b : #{b.magnitude}\n\n"
puts "magnitude value of c : #{c.magnitude}\n\n"
输出:
magnitude value of a : 111.10000000000001
magnitude value of b : 200000.0
magnitude value of c : 116.4