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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: