Ruby | BigDecimal class coerce value

BigDecimal#coerce() : coerce() is a BigDecimal class method which return the ceil value of the passed BigDecimal value.

Syntax: BigDecimal.coerce()

Parameter: numeric values (can be integer as well as BigDecimal value)



Return: An array with both BigDecimal and numeric value represented in the form of BigDecimal objects.

Code #1 : Example for coerce() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for coerce() method
  
# loading BigDecimal
require 'bigdecimal'
  
# declaring BigDecimal
a = 42.1**13
  
# declaring BigDecimal
b = -BigDecimal("10")
  
# declaring BigDecimal
c = -(22 ** 7.1) * 10
  
puts "coerce example 1 : #{a.coerce(b)}\n\n"
  
puts "coerce example 2 : #{b.coerce(c)}\n\n"

chevron_right


Output :

coerce example 1 : [-10.0, 1.3051704902006439e+21]

coerce example 2 : [#, #]

Code #2 : Example for coerce() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for coerce() method
  
# loading BigDecimal
require 'bigdecimal'
  
# declaring BigDecimal
a = 12**12 - 27
  
# declaring BigDecimal
b = BigDecimal('10')-(22 ** 7.1) ** 10
  
# declaring BigDecimal
c = BigDecimal('-3')
  
puts "coerce example 1 : #{a.coerce(b)}\n\n"
  
puts "coerce example 2 : #{b.coerce(c)}\n\n"

chevron_right


Output :

coerce example 1 : [-2.051211007305864e+95, 8916100448229.0]

coerce example 2 : [#, #]



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.