Ruby | BigDecimal to_f() function

BigDecimal#to_f() : to_f() is a BigDecimal class method which returns a new Float object having approximately the same value as the BigDecimal number.

Syntax: BigDecimal.to_f()

Parameter: BigDecimal values

Return: a new Float object having approximately the same value as the BigDecimal number.

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

BigDecimal a to_f method : 10.0

BigDecimal b to_f method : -10.0

BigDecimal c to_f method : -11.43

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

BigDecimal a to_f method : 144.0

BigDecimal b to_f method : -2.051211007305864e+95

BigDecimal c to_f method : -3.0




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.