Ruby BigDecimal to_r()函数
BigDecimal#to_r() : to_r()是BigDecimal类的一个方法,它将Big decimal的值作为一个有理数返回。
语法。BigDecimal.to_r()
参数。BigDecimal的值
返回:大十进制的值为有理数
例子 #1 :
# Ruby code for BigDecimal.to_r() 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_r() method
puts "BigDecimal a to_r method : #{a.to_r()}\n\n"
puts "BigDecimal b to_r method : #{b.to_r()}\n\n"
puts "BigDecimal c to_r method : #{c.to_r()}\n\n"
输出:
BigDecimal a to_r method : 10/1
BigDecimal b to_r method : -10/1
BigDecimal c to_r method : -1143/100
例子 #2 :
# Ruby code for BigDecimal.to_r() 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_r() method
puts "BigDecimal a to_r method : #{a.to_r()}\n\n"
puts "BigDecimal b to_r method : #{b.to_r()}\n\n"
puts "BigDecimal c to_r method : #{c.to_r()}\n\n"
输出:
“`ruby
BigDecimal a to_r method : 144/1
BigDecimal b to_r method : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990/1
BigDecimal c to_r method : -3/1
<pre><code> “`