# adding big decimal numbers

Mahesh Pinnamaneni

Ranch Hand

Posts: 100

posted 10 years ago

- 0

hi all,

here is a problem while adding big decimal numbers.can anyone help me pls.

double d1 = 999999999999.11;

double d2 = 9.11;

double d4 = (d1 + d2);

System.out.println(" Adding big double numbers:::"+d4);

// this will give me o/p as :: 1.00000000000822E12

but i am interested in getting the exact value.

pls help me.

thanks & regards,

Mahesh

here is a problem while adding big decimal numbers.can anyone help me pls.

double d1 = 999999999999.11;

double d2 = 9.11;

double d4 = (d1 + d2);

System.out.println(" Adding big double numbers:::"+d4);

// this will give me o/p as :: 1.00000000000822E12

but i am interested in getting the exact value.

pls help me.

thanks & regards,

Mahesh

posted 10 years ago

- 0

I'm surprised you're getting scientific notation for this by default, although somewhere I believe there exists a setting for this? Anyone?

Regardless, you can use the NumberFormat/DecimalFormat utility classes to display as you want:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html

By the way, you mentioned the 'exact' value, technically d4 represents the exact value, it just displays in non-useful format. If you want to perform operations on the 'exact' value, you should still use d4. Only use the formatter to display the data on the screen to the user.

[ November 09, 2005: Message edited by: Scott Selikoff ]

Regardless, you can use the NumberFormat/DecimalFormat utility classes to display as you want:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html

By the way, you mentioned the 'exact' value, technically d4 represents the exact value, it just displays in non-useful format. If you want to perform operations on the 'exact' value, you should still use d4. Only use the formatter to display the data on the screen to the user.

[ November 09, 2005: Message edited by: Scott Selikoff ]

posted 10 years ago

- 0

If you want to do

**exact**calculations with decimal numbers, you'd better use class java.math.BigDecimal instead of double. double has a limited accuracy.
posted 10 years ago

Some things you should know about floating-point arithmetic

What Every Computer Scientist Should Know About Floating-Point Arithmetic

- 0

but i am interested in getting the exact value.

Some things you should know about floating-point arithmetic

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 10 years ago

- 0

You've bumped into two problems.

First, it's not possible to represent many fractions precisely in binary. Almost no computer languages or calculators will do this: (1 / 3) * 3 = 1. On the other hand most do (1 * 3) / 3 pretty well because there are no fractions involved.

Second, floats and doubles have two parts, sometimes called significand and exponent. The value is the significand times 2 raised to the exponent power. Since you get 8 or 11 bits of exponent you can multiply out some very large numbers. But the significand has only 23 or 52 bits so you can only have so many digits before you multiply. If you have more digits of precision than fit into those bits, some digits are lost.

Floats are good for the distance between galaxies or the size of an electron, but lousy for Bill Gates's checking account.

First, it's not possible to represent many fractions precisely in binary. Almost no computer languages or calculators will do this: (1 / 3) * 3 = 1. On the other hand most do (1 * 3) / 3 pretty well because there are no fractions involved.

Second, floats and doubles have two parts, sometimes called significand and exponent. The value is the significand times 2 raised to the exponent power. Since you get 8 or 11 bits of exponent you can multiply out some very large numbers. But the significand has only 23 or 52 bits so you can only have so many digits before you multiply. If you have more digits of precision than fit into those bits, some digits are lost.

Floats are good for the distance between galaxies or the size of an electron, but lousy for Bill Gates's checking account.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

It is sorta covered in the JavaRanch Style Guide. |