Ruby BigDecimal类的绝对值
BigDecimal#abs() : abs()是BigDecimal类的一个方法,用于处理BigDecimal的数值,并将其转换为绝对形式。
语法。BigDecimal.abs()
参数。要转换为绝对值的BigDecimal值
返回:所传递的BigDecimal值的绝对值
代码#1:abs()方法的例子
# Ruby code for abs() method
# loading BigDecimal
require 'bigdecimal'
# declaring BigDecimal
a = 42.1**13
# declaring BigDecimal
b = -BigDecimal("10")
# declaring BigDecimal
c = -(22 ** 7.1) * 10
# a
puts "absolute value of a : #{a.abs}\n\n"
# b
puts "absolute value of b : #{b.abs}\n\n"
# c
puts "absolute value of c : #{c.abs}\n\n"
输出:
absolute value of a : 1.3051704902006439e+21
absolute value of b : 0.1E2
absolute value of c : 33978252067.813686
代码#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')
# a
puts "absolute value of a : #{a.abs}\n\n"
# b
puts "absolute value of b : #{b.abs}\n\n"
# c
puts "absolute value of c : #{c.abs}\n\n"
输出:
absolute value of a :8916100448229
absolute value of b : 0.205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999E96
absolute value of c : 0.3E1