当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。