當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby BigDecimal absolute用法及代碼示例


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.20512110073058639999999999999999999999999999999999999999999999999999999999999999999999999999999E96

absolute value of c:0.3E1



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | BigDecimal class absolute value。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。