BigDecimal Class in Java |

## BigDecimal Class in Java

BigDecimal supports arbitrary precision floating point rational numbers.A BigDecimal object provides a great way to represent a real number that exceeds the range or precision that can be represented by double value or the rounding that is done on a double value is acceptable.We can use them for accurate calculations like monetary or financial application .

BigDecimal representation consists of an unlimited precision integer value and an integer scale factor.The scale factor indicates a power of 10 that the integer value is implicitly divided by.

BigDecimal objects are represented as an integer of arbitrary size and an scale that represents the number of decimal places in the given BigDecimal.

BigDecimal is useful when we do comparisons and mathematical operations with other BigDecimal objects.If during operation such a division needs a rounding method,the BigDecimal defines constants to represent the different rounding method.

### How BigDecimal is represented?

If the BigDecimal value is represented by 456.7892 then the integer value is 4567892 abd scale factor is 4.

Scale factor can not be negative and BigDecimal number can not overflow.

### The structure of the BigDecimal class is given as:

```
public class java.math.BigDecimal extends
java.lang.Number {
```**//Constructors**
public BigDecimal(String val) throws NumberFormatException;
public BigDecimal(double val) throws NumberFormatException;
public BigDecimal(BigInteger val);
public BigDecimal(BigInteger val, int scale) throws
NumberFormatException;
**// Constants**
public static final int ROUND_CEILING;
public static final int ROUND_DOWN;
public static final int ROUND_FLOOR;
public static final int ROUND_HALF_DOWN;
public static final int ROUND_HALF_EVEN;
public static final int ROUND_HALF_UP;
public static final int ROUND_UNNECESSARY;
public static final int ROUND_UP;
**//Methods**
public static BigDecimal valueOf(long val, int scale)
throws NumberFormatException;
public BigDecimal abs();
public BigDecimal add(BigDecimal val);
public int compareTo(BigDecimal val);
public BigDecimal divide(BigDecimal val, int scale, int roundingMode) throws
ArithmeticException, IllegalArgumentException;
public BigDecimal divide(BigDecimal val, int roundingMode) throws
ArithmeticException, IllegalArgumentException;
public double doubleValue();
public boolean equals(Object x);
public float floatValue();
public int hashCode();
public int intValue();
public long longValue();
public BigDecimal max(BigDecimal val);
public BigDecimal min(BigDecimal val);
public BigDecimal movePointLeft(int n);
public BigDecimal movePointRight(int n);
public BigDecimal multiply(BigDecimal val);
public BigDecimal negate();
public int scale();
public BigDecimal setScale(int scale, int roundingMode) throws
ArithmeticException, IllegalArgumentException;
public BigDecimal setScale(int scale) throws ArithmeticException,
IllegalArgumentException;
public int signum();
public BigDecimal subtract(BigDecimal val);
public BigInteger toBigInteger();
public String toString();
}

`public BigDecimal(String val) throws NumberFormatException;`

Creates a BigDecimal number with the given string specified

in the argument.

The String val construction rule:

- It can have optional minus sign followed by zero and

more decimal digits followed by an optional franction

like(-xyz.mno) - The fraction must contain a decimal point and zero or

more decimal digits. - The String must contain at least one digit in the integer

part or fractional part. - The scale of the BigDecimal is created is equal to the number of the digits to the right of the decimal point or ‘0’ if there is no decimal point.
- The mapping from character to digit is performed by characeter.digit() method.
- If the String can not be parsed to valid BigDecimal,we get the NumberFormatException.

public BigDecimal(double val) throws NumberFormatException;

Creates a BigDecimal number with the given value- val.The scale of this number is the smallest value such that 10 to the power scale x value- it represents the integer. If the double value represents Double.NEGATIVE_INFINITY

Double.POSITIVE_INFINITY or Double.NaN NumberFormatException is thrown.

public BigDecimal(BigInteger val);

Creates a BigDecimal from BigInteger. The scale of the BigDecimal is zero.(0).

public BigDecimal(BigInteger val, int scale) throws NumberFormatException;

Creates a BigDecimal from BigInteger and Scale. The scale specifies how many digits of the BigInteger fall right of the decimal point. It throws NumberFormatException if the scale is negative.

BigDecimal is zero.(0).

public static final int ROUND_CEILING;

A rounding method towards positive infinity.The value is rounded to the least integer greater than equal to its value.

Example 3.5 is rounded to 4 and -3.5 is rounded to -3

public static final int ROUND_DOWN;

A rounding method towards zero.This is used during truncation.

Example 3.5 is rounded to 5 and -3.5 is rounded to -3

public static final int ROUND_FLOOR;

A rounding method towards negative infinity.The value is rounded to the greater integer less than equal to its value.

Example 3.5 is rounded to 3 and -3.5

is rounded to -4

public static final int ROUND_HALF_DOWN;

A rounding method where the digit prior to a discarded fraction if the fraction is greater than .5,otherwise the the digit remains same.

Example- 3.5 rouns to 3,3.51 rounds to 4

-3.5 rounds to -3 and -3.54 rounds to -4

public static final int ROUND_HALF_EVEN;

A rounding method that follows if the digit prior to the discarded fraction is odd. Otherwise it follows the ROUND_HALF_DOWN.

Example 2.5 rounds to 2,3.5 rounds to 4

-3.5 rounds to -3 and -3.5 rounds to -4

public static final int ROUND_HALF_UP;

A rounding method that increments the digit prior to a discarded fraction if the fraction is greater than or equal to .5,otherwise the digit is unchanged.

Example-3.5 rounds to 4,3.49 rounds to 3

-3.5 rounds to -4 and -3.49 rounds to -3

public static final int ROUND_UNNECESSARY;

A constant that signifies that rounding is not necessary.If the result really does require rounding,an ArithmeticException is thrown.

public static final int ROUND_UP;

A rounding method that rounds away from zero by truncating.

Example- 3.5 rounds to 4

-3.5 rounds to -4

public static BigDecimal valueOf(long val, int scale) throws NumberFormatException;

Returns the BigDecimal value from the given number long type and scale specified. The scale specifies how many digits falls to the right of the decimal point. If scale is negative(<0) , it throws NumberFormatException.

public static BigDecimal valueOf(long val);

Returns the BigDecimal value from the given number long type. The scale created for this number is zero.

public BigDecimal abs();

It returns the absolute value of the BigDecimal number.If the BigDecimal number is positive,it is returned as it is. If it is negative,then a new BigDecimal number is returned . The new number contains this BigDecimal number. The scale is same for both new nad old BigDecimal number.

public BigDecimal add(BigDecimal val);

Returns the sum of two BigDecimal numbers and put into a

new BigDecimal number.The scale is the maximum of the two

numbers.

public int compareTo(BigDecimal val);

This method compares this BigDecimal value to a given BigDecimal value and returns the value indicating the result of the comparison. The two bigDecimals if have same value but different scales are considered as equal.