BigDecimal class:
All data types in java have prior rounding function/mode and have a range to represent a number .There are many problems when we dealing with currency or when we want rounding function with respect to problem. BigDecimal class solves this problem. It has 8 different rounding mode. All these are static and cal be called with class name and have almost unlimited range to represent a number.
BigDecimal add() function:
BigDecimal class provides 2 different methods for addition. All these functions return a object of BigDecimal class.
Now write the following code for Addition on BigDecimal variables.
Code:
import java.util.Scanner;
import java.math.BigDecimal;
public class ArithmaticOperation {
public static void main(String args[]) {
// set up some variables
BigDecimal num1 = new BigDecimal("789012345.768273648");
BigDecimal num2 = new BigDecimal("20");
num1 = num1.add(num2);
System.out.println("Addition = " + num1);
}
}
This is simple code for Add BigDecimal variables/values using BigDecimal class in Java
Get This Book Now

