Ruby Float zero?()方法及示例
Float#zero() : zero() 是一个float类的方法,用于检查浮点数是否为零。
语法: float.zero()
参数: 要检查的浮点数值
返回: 布尔值 如果值为零,则返回真,否则返回假
例子 #1 :
# Ruby code for zero?() method
# Initializing value
a = -100.7 * 0
b = -0
c = (22 + 7.1) * 4
# Printing a
puts "a is zero : #{a.zero?}\n\n"
# Printing b
puts "b is zero : #{b.zero?}\n\n"
# Printing c
puts "c is zero : #{c.zero?}\n\n"
输出:
a is zero : true
b is zero : true
c is zero : false
例子#2。
# Ruby code for zero?() method
# Initializing value
a = 2.0
b = 0.000000001
# Printing result
puts "a is zero : #{a.zero?}\n\n"
puts "b is zero : #{b.zero?}\n\n"
输出:
a is zero : false
b is zero : false