📜  红宝石 | BigDecimal round()函数

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

红宝石 | BigDecimal round()函数

BigDecimal#round() :round()是一个 BigDecimal 类方法,它将 Big Decimal 舍入到最接近的整数。

示例 #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