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


Ruby BigDecimal round()用法及代碼示例


BigDecimal#round():round()是BigDecimal類方法,該方法將Big十進製四舍五入到最接近的整數。

用法:BigDecimal.round()

參數:BigDecimal值


返回:將大十進製四舍五入到最接近的整數。

示例1:

# Ruby code for BigDecimal.round() method 
  
# loading library 
require 'bigdecimal'
  
# declaring bigdecimal 
a = 42.1**13
  
# declaring bigdecimal 
b = -BigDecimal("10") 
  
# declaring bigdecimal 
c = -(22 ** 7.1) * 10
  
# round() method 
puts "BigDecimal round example : #{a.round()}\n\n"
  
puts "BigDecimal round example : #{b.round()}\n\n"
  
puts "BigDecimal round example : #{c.round()}\n\n"

輸出:

BigDecimal round example : 1305170490200643862528

BigDecimal round example : -10

BigDecimal round example : -33978252068

示例2:

# Ruby code for BigDecimal.round() method 
  
# loading library 
require 'bigdecimal'
  
# declaring bigdecimal 
a = 12**12 - 27
  
# declaring bigdecimal 
b = BigDecimal('10')-(22 ** 7.1) ** 10
  
# declaring bigdecimal 
c = BigDecimal('-3') 
  
# round() method 
puts "BigDecimal round example : #{a.round()}\n\n"
  
puts "BigDecimal round example : #{b.round()}\n\n"
  
puts "BigDecimal round example : #{c.round()}\n\n"

輸出:


BigDecimal round example : 8916100448229

BigDecimal round example : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990

BigDecimal round example : -3


相關用法


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