Ruby BigDecimal绝对值
BigDecimal#abs() : abs()是BigDecimal类的一个方法,它对BigDecimal值进行处理,并将其转换为绝对形式。
语法。 BigDecimal.abs()
参数。 要转换为绝对值的大十进制数值
返回:所传递的BigDecimal值的绝对值
代码#1:abs()方法的例子
# Ruby code for abs() method
# loading BigDecimal
require 'bigdecimal'
# declaring BigDecimal
a = 42**13
# declaring BigDecimal
b = -BigDecimal("10")
# declaring BigDecimal
c = -(22 + 7.1) ** 10
# absolute value
puts "absolute value of a : #{a.abs}\n\n"
# absolute value
puts "absolute value of b : #{b.abs}\n\n"
# absolute value
puts "absolute value of c : #{c.abs}\n\n"
输出:
absolute value of a : 56.23333333
absolute value of b : 10000.0
absolute value of c : 29.1
代码#2:abs()方法的例子
# Ruby code for abs() method
# loading BigDecimal
require 'bigdecimal'
# declaring BigDecimal
a = 12**12 - 27
# declaring BigDecimal
b = BigDecimal('10')-(22 + 7.1) ** 10
# declaring BigDecimal
c = BigDecimal('-3')
# absolute value
puts "absolute value of a : #{a.abs}\n\n"
# absolute value
puts "absolute value of b : #{b.abs}\n\n"
# absolute value
puts "absolute value of c : #{c.abs}\n\n"
输出:
absolute value of a : 111.10000000000001
absolute value of b : 200000.0
absolute value of c : 116.4