📜  红宝石 | BigDecimal 绝对值

📅  最后修改于: 2022-05-13 01:55:24.242000             🧑  作者: Mango

红宝石 | BigDecimal 绝对值

BigDecimal#abs() : abs()是一个 BigDecimal 类方法,它适用于 BigDecimal 值并将它们转换为绝对形式。

Syntax:  BigDecimal.abs()

Parameter:  BigDecimal value which is to be converted to absolute value

Return:  absolute value of the passed BigDecimal value

代码 #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