Java BigDecimal Class Methods with Examples

In this article, we will discuss important methods or APIs of the Java BigDecimal class from the java.math package.
The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.
The toString() method provides a canonical representation of a BigDecimal. It gives the user complete control over rounding behavior.
Two types of operations are provided for manipulating the scale of a BigDecimal
  • scaling/rounding operations
  • decimal point motion operations
Check out below Java Core API packages tutorials:

Java BigDecimal Class Diagram

The below class diagram shows a list of methods that the BigDecimal class provides.

Java BigDecimal Class Methods/APIs with Examples

Let's demonstrates the usage of a few important and commonly used BigDecimal class methods with examples. In below BigDecimalMethodsExample class, each method name describes the BigDecimal class method and its usage. Check out each BigDecimal class method description at Oracle Java API Doc.
package net.javaguides.math;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

/**
 * Class demonstrates the usage of BigDecimal class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class BigDecimalMethodsExample {
    public static void main(String[] args) {
        BigDecimalMethodsExample example = new BigDecimalMethodsExample();

        example.valueOfLongMethod();
        example.valueOfMethod();
        example.toStringMethod();
        example.toBigIntegerMethod();
        example.subtractMethod();
        example.signumMethod();
        example.roundMethod();
        example.remainderMethod();
        example.precisionMethod();
        example.powMethod();
        example.plusMethod();
        example.negateMethod();
        example.multiplyMethod();
        example.minMethod();
        example.maxMethod();
        example.longValueMethod();
        example.intValueMathod();
        example.floatValueMethod();
        example.doubleValueMethod();
        example.divideMethod();
        example.addMethod();
        example.absMethod();
    }
    public void valueOfLongMethod() {
        // create a Long Object
        Long l = new Long("12345678");

        // assign the bigdecimal value of l to bg
        // static method is called using class name
        BigDecimal bg = BigDecimal.valueOf(l);

        String str = "The Long After Conversion to BigDecimal is " + bg;

        // print bg value
        System.out.println(str);
    }

    public void valueOfMethod() {
        // create a BigDecimal object
        BigDecimal bg;

        // create a Long Object
        Long l = new Long("12345678");

        // assign the bigdecimal value of l to bg
        // scale is 4
        bg = BigDecimal.valueOf(l, 4);

        String str = "The Value of BigDecimal using scale 4 is " + bg;

        // print bg value
        System.out.println(str);
    }

    public void toStringMethod() {
        // create a BigDecimal object
        BigDecimal bg;

        // create a String object
        String s;

        MathContext mc = new MathContext(3); // 3 precision

        bg = new BigDecimal("1234E4", mc);

        // assign the string value of bg to s
        s = bg.toString();

        String str = "String value of " + bg + " is " + s;

        // print s value
        System.out.println(str);

    }

    public void toBigIntegerMethod() {
        // create a BigDecimal object
        BigDecimal bg1;

        // create a BigInteger object
        BigInteger i1;

        bg1 = new BigDecimal("235.738");

        // assign the BigInteger value of bg1 to i1
        i1 = bg1.toBigInteger();

        String str = "BigInteger value of " + bg1 + " is " + i1;

        // print i1 value
        System.out.println(str);
    }

    public void subtractMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("100.123");
        bg2 = new BigDecimal("50.56");

        // subtract bg1 with bg2 and assign result to bg3
        bg3 = bg1.subtract(bg2);

        String str = "The Result of Subtraction is " + bg3;

        // print bg3 value
        System.out.println(str);

    }

    public void signumMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        // create 3 int objects
        int i1, i2, i3;

        bg1 = new BigDecimal("123");
        bg2 = new BigDecimal("0");
        bg3 = new BigDecimal("-12");

        // assign the signum values of bg1,bg2,bg3 to i1,i2,i3 respectively
        i1 = bg1.signum();
        i2 = bg2.signum();
        i3 = bg3.signum();

        String str1 = "The Result of Signum function on " + bg1 + " is " + i1;
        String str2 = "The Result of Signum function on " + bg2 + " is " + i2;
        String str3 = "The Result of Signum function on " + bg3 + " is " + i3;

        // print i1,i2,i3 values
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }

    public void roundMethod() {
        // create 2 BigDecimal Objects
        BigDecimal bg1, bg2;

        bg1 = new BigDecimal("5.46497");

        MathContext mc = new MathContext(3); // 3 precision

        // bg1 is rounded using mc
        bg2 = bg1.round(mc);

        String str = "The value " + bg1 + " after rounding is " + bg2;

        // print bg2 value
        System.out.println(str);
    }

    public void remainderMethod() {
        // create 3 BigDecimal Objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("513.54");
        bg2 = new BigDecimal("5");

        // bg2 divided by bg1 gives bg3 as remainder
        bg3 = bg1.remainder(bg2);

        String str = "The remainder is " + bg3;

        // print the value of bg3
        System.out.println(str);
    }

    public void precisionMethod() {
        // create 2 BigDecimal Objects
        BigDecimal bg1, bg2;

        bg1 = new BigDecimal("123.234");
        bg2 = new BigDecimal("523");

        // create 2 int objects
        int i1, i2;

        // assign the result of precision of bg1, bg2 to i1 and i2
        i1 = bg1.precision();
        i2 = bg2.precision();

        String str1 = "The precision of " + bg1 + " is " + i1;
        String str2 = "The precision of " + bg2 + " is " + i2;

        // print the values of i1, i2
        System.out.println(str1);
        System.out.println(str2);
    }

    public void powMethod() {
        // create 2 BigDecimal Objects
        BigDecimal bg1, bg2;

        bg1 = new BigDecimal("2.17");

        // apply pow method on bg1
        bg2 = bg1.pow(3);

        String str = "The value of " + bg1 + " to the power of 3 is " + bg2;

        // print bg2 value
        System.out.println(str);
    }

    public void plusMethod() {
        // create 2 BigDecimal Objects
        BigDecimal bg1, bg2;

        // assign value to bg1
        bg1 = new BigDecimal("-123.126");

        // assign the result of plus method on bg1 to bg2
        bg2 = bg1.plus();

        String str = "The value of the BigDecimal is " + bg2;

        // print the value of bg2
        System.out.println(str);
    }

    public void negateMethod() {
        // create 2 BigDecimal objects
        BigDecimal bg1, bg2;

        // assign value to bg1
        bg1 = new BigDecimal("40.1264");

        // assign negate value of bg1 to bg2
        bg2 = bg1.negate();

        String str = "Negated value of " + bg1 + " is " + bg2;

        // print bg1 and bg2 values
        System.out.println(str);
    }

    public void multiplyMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("2.310");
        bg2 = new BigDecimal("4.620");

        // multiply bg1 with bg2 and assign result to bg3
        bg3 = bg1.multiply(bg2);

        String str = "Multiplication Result is " + bg3;

        // print bg3 value
        System.out.println(str);
    }

    public void minMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("235");
        bg2 = new BigDecimal("236");

        // assign the min value of bg1, bg2 to bg3
        bg3 = bg1.min(bg2);

        String str = "Minimum Value among " + bg1 + " and " + bg2 + " is " + bg3;

        // print the minimum value
        System.out.println(str);
    }

    public void maxMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("235");
        bg2 = new BigDecimal("236");

        // assign the max value of bg1, bg2 to bg3
        bg3 = bg1.max(bg2);

        String str = "Maximum Value among " + bg1 + " and " + bg2 + " is " + bg3;

        // print the maximum value
        System.out.println(str);
    }

    public void longValueMethod() {
        // create 2 BigDecimal objects
        BigDecimal bg1, bg2;

        // create 2 long objecs
        long l1, l2;

        bg1 = new BigDecimal("429.07");
        bg2 = new BigDecimal("429496732223453626252");

        // assign the long value of bg1 and bg2 to l1,l2 respectively
        l1 = bg1.longValue();
        l2 = bg2.longValue();

        String str1 = "long value of " + bg1 + " is " + l1;
        String str2 = "long value of " + bg2 + " is " + l2;

        // print l1,l2 values
        System.out.println(str1);
        System.out.println(str2);
    }

    public void intValueMathod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2;

        // Create 2 int Object
        int i1, i2;

        bg1 = new BigDecimal("1234");

        // assign a larger value to bg2
        bg2 = new BigDecimal("3383878445");

        // assign the int value of bg1 and bg2 to i1,i2 respectively
        i1 = bg1.intValue();
        i2 = bg2.intValue();

        String str1 = "int value of " + bg1 + " is " + i1;
        String str2 = "int value of " + bg2 + " is " + i2;

        // print i1,i2
        System.out.println(str1);
        System.out.println(str2);
    }

    public void floatValueMethod() {
        // create a BigDecimal object
        BigDecimal bg;

        // create a Float object
        Float f;

        bg = new BigDecimal("1234.123486");

        // assign the converted value of bg to f
        f = bg.floatValue();

        String str = "Float value of " + bg + " is " + f;

        // print f value
        System.out.println(str);
    }

    public void doubleValueMethod() {
        // create a BigDecimal object
        BigDecimal bg;

        // create a Double object
        Double d;

        bg = new BigDecimal("1234");

        // assign the converted value of bg to d
        d = bg.doubleValue();

        String str = "Double value of " + bg + " is " + d;

        // print d value
        System.out.println(str);
    }

    public void divideMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        bg1 = new BigDecimal("45");
        bg2 = new BigDecimal("5");

        bg3 = bg1.divide(bg2); // divide bg1 with bg2

        String str = "Division result is " + bg3;

        // print bg3 value
        System.out.println(str);
    }

    public void addMethod() {
        // create 3 BigDecimal objects
        BigDecimal bg1, bg2, bg3;

        // assign value to bg1 and bg2
        bg1 = new BigDecimal("40.732");
        bg2 = new BigDecimal("30.12");

        // print bg1 and bg2 value
        System.out.println("Object Value is " + bg1);
        System.out.println("Augend value is " + bg2);

        // perform add operation on bg1 with augend bg2
        bg3 = bg1.add(bg2);

        // print bg3 value
        System.out.println("Result is " + bg3);
    }

    public void absMethod() {
        // create 2 BigDecimal objects
        BigDecimal bg1, bg2;

        // assign value to bg1
        bg1 = new BigDecimal("-40");

        // value before applying abs
        System.out.println("Value is " + bg1);

        // assign absolute value of bg1 to bg2
        bg2 = bg1.abs();

        // print bg2 value
        System.out.println("Absolute value is " + bg2);
    }
}
Output:
The Long After Conversion to BigDecimal is 12345678
The Value of BigDecimal using scale 4 is 1234.5678
String value of 1.23E+7 is 1.23E+7
BigInteger value of 235.738 is 235
The Result of Subtraction is 49.563
The Result of Signum function on 123 is 1
The Result of Signum function on 0 is 0
The Result of Signum function on -12 is -1
The value 5.46497 after rounding is 5.46
The remainder is 3.54
The precision of 123.234 is 6
The precision of 523 is 3
The value of 2.17 to the power of 3 is 10.218313
The value of the BigDecimal is -123.126
Negated value of 40.1264 is -40.1264
Multiplication Result is 10.672200
Minimum Value among 235 and 236 is 235
Maximum Value among 235 and 236 is 236
long value of 429.07 is 429
long value of 429496732223453626252 is 5221618528133939084
int value of 1234 is 1234
int value of 3383878445 is -911088851
Float value of 1234.123486 is 1234.1235
Double value of 1234 is 1234.0
Division result is 9
Object Value is 40.732
Augend value is 30.12
Result is 70.852
Value is -40
Absolute value is 40

Comments