Ruby | BigDecimal to_digits() function

BigDecimal#to_digits() : to_digits() is a BigDecimal class method which converts a Big decimal to string of the form “nnnnnn.mmm”

Syntax: BigDecimal.to_digits()

Parameter: BigDecimal values

Return: converts a Big decimal to string of the form “nnnnnn.mmm”

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for BigDecimal.to_digits() 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_digits() method
puts "BigDecimal a to_digits method : #{a.to_digits()}\n\n"
  
puts "BigDecimal b to_digits method : #{b.to_digits()}\n\n"
  
puts "BigDecimal c to_digits method : #{c.to_digits()}\n\n"

chevron_right


Output :

BigDecimal a to_digits method : 10.0

BigDecimal b to_digits method : -10.0

BigDecimal c to_digits method : -11.43

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for BigDecimal.to_digits() 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_digits() method
puts "BigDecimal a to_digits method : #{a.to_digits()}\n\n"
  
puts "BigDecimal b to_digits method : #{b.to_digits()}\n\n"
  
puts "BigDecimal c to_digits method : #{c.to_digits()}\n\n"

chevron_right


Output :

BigDecimal a to_digits method : 144.0

BigDecimal b to_digits method : -205121100730586399999999999999999999999999999999999999999999999999999999999999999999999999999990.0

BigDecimal c to_digits 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.