Ruby | BigDecimal absolute value

BigDecimal#abs() : abs() is a BigDecimal class method which works on BigDecimal values and converts them to the absolute form.

Syntax:  BigDecimal.abs()

Parameter:  BigDecimal value which is to be converted to absolute value

Return:  absolute value of the passed BigDecimal value

Code #1 : Example for abs() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for abs() method
    
# loading BigDecimal
require 'bigdecimal'
    
# declaring BigDecimal
a = 42**13
    
# declaring BigDecimal
b = -BigDecimal("10")
    
# declaring BigDecimal
c = -(22 + 7.1) ** 10
  
# absolute value
puts "absolute value of a : #{a.abs}\n\n"
  
# absolute value
puts "absolute value of b : #{b.abs}\n\n"
  
# absolute value
puts "absolute value of c : #{c.abs}\n\n"

chevron_right


Output :

absolute value of a : 56.23333333

absolute value of b : 10000.0

absolute value of c : 29.1

Code #2 : Example for abs() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for abs() 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')
  
# absolute value
puts "absolute value of a : #{a.abs}\n\n"
  
# absolute value
puts "absolute value of b : #{b.abs}\n\n"
  
# absolute value
puts "absolute value of c : #{c.abs}\n\n"

chevron_right


Output :

absolute value of a : 111.10000000000001

absolute value of b : 200000.0

absolute value of c : 116.4



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.