Ruby | BigDecimal to_r() function
BigDecimal#to_r() : to_r() is a BigDecimal class method which returns the value of Big decimal as a rational number.
Syntax: BigDecimal.to_r()
Parameter: BigDecimal values
Return: the value of Big decimal as a rational number
Example #1 :
# Ruby code for BigDecimal.to_r() method # loading libraryrequire 'bigdecimal'require 'bigdecimal/util' # declaring bigdecimala = BigDecimal("10") # declaring bigdecimalb = -BigDecimal("10") # declaring bigdecimalc = -BigDecimal("11.43") # to_r() methodputs "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" |
Output :
BigDecimal a to_r method : 10/1 BigDecimal b to_r method : -10/1 BigDecimal c to_r method : -1143/100
Example #2 :
# Ruby code for BigDecimal.to_r() method # loading libraryrequire 'bigdecimal'require 'bigdecimal/util' # declaring bigdecimala = BigDecimal('12')*12 # declaring bigdecimalb = BigDecimal('10')-(22 ** 7.1) ** 10 # declaring bigdecimalc = BigDecimal('-3') # to_r() methodputs "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" |
Output :
BigDecimal a to_r method : 144/1BigDecimal b to_r method : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990/1
BigDecimal c to_r method : -3/1

