Skip to content
Related Articles

Related Articles

Improve Article
Ruby | BigDecimal to_digits() function
  • Last Updated : 06 Dec, 2019

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 :






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

Output :

BigDecimal a to_digits method : 10.0

BigDecimal b to_digits method : -10.0

BigDecimal c to_digits method : -11.43

Example #2 :




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

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
Recommended Articles
Page :