Ruby Float positive()方法及示例
float positive()是一个float类方法,用于检查浮点数是否为正数。
语法: float.positive()
参数: 要检查的浮点数
返回: 布尔值 返回true – 如果值是正的,或者返回false – 如果值是负的。
例子 #1 :
# Ruby program for positive() method
# Initializing value
a = -100.7 - 10.4
b = -100 * 2000.0
c = (22 + 7.1) * 4
# Printing result
puts "a is positive : #{a.positive?}\n\n"
puts "b is positive : #{b.positive?}\n\n"
puts "c is positive : #{c.positive?}\n\n"
输出:
a is positive : false
b is positive : false
c is positive : true
例子#2 。
# Ruby code for positive() method
# Initializing value
a = -56.23333333
b = 10000.0
c = -(22 + 7.1)
# Printing result
puts "a is positive : #{a.positive?}\n\n"
puts "b is positive : #{b.positive?}\n\n"
puts "c is positive : #{c.positive?}\n\n"
输出:
a is positive : false
b is positive : true
c is positive : false