Open In App

Ruby | BigDecimal class absolute value

Last Updated : 08 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Output :

absolute value of a : 1.3051704902006439e+21

absolute value of b : 0.1E2

absolute value of c : 33978252067.813686

Code #2 : Example for abs() method




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


Output :

absolute value of a : 8916100448229

absolute value of b : 0.20512110073058639999999999999999999999999999999999999999999999999999999999999999999999999999999E96

absolute value of c : 0.3E1



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads