Coding Villa

Add BigDecimal variables/values using BigDecimal class in Java

Author : Mehreen Jamil

Share with Friends
Views : 530
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can Add BigDecimal variables/values using BigDecimal class in Java.


Advertisements


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

Java 2 Database Programming for Dummies Java 2 Database Programming for Dummies Java 2 Database Programming for Dummies Java 2 Dat



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook