📜  红宝石 | BigDecimal to_f()函数

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

红宝石 | BigDecimal to_f()函数

BigDecimal#to_f() : to_f()是一个 BigDecimal 类方法,它返回一个新的 Float 对象,该对象的值与 BigDecimal 数大致相同。

示例 #1:

# Ruby code for BigDecimal.to_f() method
  
# loading library
require 'bigdecimal'
require 'bigdecimal/util'
  
# declaring bigdecimal
a = BigDecimal("10")
  
# declaring bigdecimal
b = -BigDecimal("10")
  
# declaring bigdecimal
c = -BigDecimal("11.43")
  
# to_f() method
puts "BigDecimal a to_f method : #{a.to_f()}\n\n"
  
puts "BigDecimal b to_f method : #{b.to_f()}\n\n"
  
puts "BigDecimal c to_f method : #{c.to_f()}\n\n"

输出 :

BigDecimal a to_f method : 10.0

BigDecimal b to_f method : -10.0

BigDecimal c to_f method : -11.43

示例 #2:

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

输出 :

BigDecimal a to_f method : 144.0

BigDecimal b to_f method : -2.051211007305864e+95

BigDecimal c to_f method : -3.0