Monday, May 3, 2010

BigDecimal is very important class that java API provides. Its mainly used for Calculation purposes.
In Banking and Accounting application the use of BigDecimal Class is compulsory.
Java is not good at handling floating point numbers basically. without scaling its annoying to see the number. Without any scaling U may get number up to 49 decimal digits :(
So to use scaling of 2 decimal digits we got to use setScale() method of BigDecimal Class
The below example illustrates it.


Setting Scale for BigDecimal in JAVA

package core.java;
import java.math.BigDecimal;


public class BigDecimalDemo {
public static void main(String args[])
{
BigDecimal bigDecimalNum=new BigDecimal(10.49);
System.out.println(bigDecimalNum);
bigDecimalNum=bigDecimalNum.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println(bigDecimalNum);
}
}

The output of the above program is show below.
10.4900000000000002131628207280300557613372802734375
10.49

1 comment:

  1. Hey dis is realy informative one ..
    nice 2 u see u juniors blogging dat to on java :)
    keep it up.. would like to see more on the same.

    ReplyDelete