红宝石 |浮点截断函数
Float#truncate()是一个浮点类方法,它返回舍入到 ndigits 十进制数字精度的截断值。
Syntax: float.truncate()
Parameter: float value as argument
Return: truncated value rounded to nearest precision
If precision is -ve : integer with at least ndigits.abs trailing zeros
If ndigits is +ve : a floating point number, otherwise integer
示例 #1:
# Ruby code for truncate() method
# declaring float values
a = 0.767
# declaring float values
b = 2999.011
# declaring float values
c = 2.0000
# TRUNCATED VALUES
puts "truncate a : #{a.truncate()}\n\n"
puts "truncate b : #{b.truncate()}\n\n"
puts "truncate c : #{c.truncate()}\n\n"
输出 :
truncate a : 0
truncate b : 2999
truncate c : 2
示例 #2:
# Ruby code for truncate() method
# declaring float values
a = -83930.00000
# declaring float values
b = -66662999.11
# TRUNCATED VALUES
puts "truncate a : #{a.truncate()}\n\n"
puts "truncate b : #{b.truncate()}\n\n"
输出 :
truncate a : -83930
truncate b : -66662999