Archive for the ‘java’ Category

h1

Why use Big Decimal in java

July 11, 2010

When we work with decimals in certain financial related calculations, we see that certain numbers get rounded off incorrectly even though they are stored in double. Below is the example:

public static void main(String o[])
{
double d =3.14159265358979353d;
System.out.println(d);
}

Output is :

3.1415926535897936

It makes sense to have the value as 3.1415926535897935 but not the one that is printed. The reason is double cannot store the right precision for floating point numbers and if you need to store the precision correctly, you need to use Big Decimal. A very good article of using big decimal can be found here

Advertisements