Ruby Integer to_d() function with example

The to_d function in Ruby converts the value of int as a BigDecimal.

Syntax: number.to_d

Parameter: The function takes the integer which is to be converted to BigDecimal.



Return Value: The function returns the BigDecimal value of int.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_d function 
   
require 'bigdecimal'
require 'bigdecimal/util'
   
# Initializing the number
num1 = 5
num2 = 10 ** 100 
num3 = 1678 
num4 = 189790
    
# Prints the int as big-decimal 
puts num1.to_d
puts num2.to_d
puts num3.to_d
puts num4.to_d

chevron_right


Output:

 0.5e1
0.1e101
0.1678e4
0.18979e6

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for to_d function 
   
require 'bigdecimal'
require 'bigdecimal/util'
   
# Initializing the number
num1 = 5
num2 = 19**18
num3 = 2**45
num4 = 888
    
# Prints the int as big-decimal 
puts num1.to_d
puts num2.to_d
puts num3.to_d
puts num4.to_d

chevron_right


Output:

0.5e1
0.104127350297911241532841e24
0.35184372088832e14
0.888e3


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.