Ruby Float to_d() – BigDecimal method with example

Float to_d() is a float class method which return a BigDecimal representation of the float value

Syntax: float.to_d()

Parameter: float value as argument
precision parameter : number of significant digits for the result

Return: BigDecimal representation of the float value with a significant number of digits for the result.

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

BigDecimal a : 0.767E0

BigDecimal b : 0.2999011E4

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for to_d() method
  
require 'bigdecimal'
require 'bigdecimal/util'
  
# Initializing value
a = 0.767
b = 2999.011
c = 2.0000
  
# Printing reult
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"

chevron_right


Output :

BigDecimal a : 0.77E0

BigDecimal b : 0.2999E4

BigDecimal c : 0.2E1

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.