Java abs() Method : Find Absolute Value of Decimal MathContext
Java - Find Absolute Value of Decimal :
Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.
Declaration
1 |
public BigDecimal abs(MathContext mc) |
Parameters
Parameters | |
mc | the context to use. |
Return Value
BigDecimal | This method returns the absolute value of the called value i.e abs(this) rounded as necessary. |
Exception
ArithmeticException | if the result is inexact but the rounding mode is UNNECESSARY. |
Example
Below is the example for the math.BigDecimal.abs() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.c4learn.maths; import java.math.*; public class BigDecimalExample { public static void main(String[] args) { // create object of BigDecimal class BigDecimal bd1, bd2; // assign value to bg1 bd1 = new BigDecimal("-100"); // before calling abs() System.out.println("Value is " + bd1); // assign absolute value of bd1 to bd2 bd2 = bd1.abs(); // print bg2 value System.out.println("Absolute value is " + bd2); } } |
Output :
1 2 |
Value is -100 Absolute value is 100 |