📜  Ruby Float to_d() – BigDecimal 方法示例

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

Ruby Float to_d() – BigDecimal 方法示例

Float to_d()是一个浮点类方法,它返回浮点值的 BigDecimal 表示

示例 #1:

Ruby
# Ruby code for to_d() method
 
require 'bigdecimal'
require 'bigdecimal/util'
 
# Initializing value
a = 0.767
b = 2999.011
 
# Printing result
puts "BigDecimal a : #{a.to_d}\n\n"
puts "BigDecimal b : #{b.to_d}\n\n"


Ruby
# Ruby code for to_d() method
 
require 'bigdecimal'
require 'bigdecimal/util'
 
# Initializing value
a = 0.767
b = 2999.011
c = 2.0000
 
# Printing result
puts "BigDecimal a : #{a.to_d(2)}\n\n"
puts "BigDecimal b : #{b.to_d(5)}\n\n"
puts "BigDecimal c : #{c.to_d()}\n\n"


输出 :

BigDecimal a : 0.767E0

BigDecimal b : 0.2999011E4

示例 #2:

红宝石

# Ruby code for to_d() method
 
require 'bigdecimal'
require 'bigdecimal/util'
 
# Initializing value
a = 0.767
b = 2999.011
c = 2.0000
 
# Printing result
puts "BigDecimal a : #{a.to_d(2)}\n\n"
puts "BigDecimal b : #{b.to_d(5)}\n\n"
puts "BigDecimal c : #{c.to_d()}\n\n"

输出 :

BigDecimal a : 0.77E0

BigDecimal b : 0.2999E4

BigDecimal c : 0.2E1