mike cool

Greenhorn

Posts: 24

posted 12 years ago

hey

double d1 = 5555.55;

double d2 = 1111.11;

if i subtract d2 from d1

double d3 = d1 - d2;

the result will be : Sub is: 4444.4400000000005 ;

how can i control the decimal amount for example i want have only 3

digits after the dot Sub is: 4444.440

but not using output format

i try bigDecimal , math class but i couldn't find the solution

any idea

double d1 = 5555.55;

double d2 = 1111.11;

if i subtract d2 from d1

double d3 = d1 - d2;

the result will be : Sub is: 4444.4400000000005 ;

how can i control the decimal amount for example i want have only 3

digits after the dot Sub is: 4444.440

but not using output format

i try bigDecimal , math class but i couldn't find the solution

any idea

todd runstein

Ranch Hand

Posts: 64

posted 12 years ago

Ain't floating point math great?

I just ran into this and used NumberFormat to solve this (another advantage in my case was that I got a String, which is what I utimately needed). You can set the min and max number of decimal spaces. May not be the best way, but it works.

I just ran into this and used NumberFormat to solve this (another advantage in my case was that I got a String, which is what I utimately needed). You can set the min and max number of decimal spaces. May not be the best way, but it works.

posted 12 years ago

Just as 1/3 cannot be written within a finite number of numbers, computers face the same problem. However, computers can only exactly store fractions that have only powers of 2 in them:

x1 * 1/2 + x2 * 1/4 + x3 * 1/8 + ....

Even something as simple as 1/10 (0.1) cannot be stored exactly because it has a repetitive part:

1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + ...

Which is in binary 0.000110011001100110011001100...

Somewhere, this gets rounded of course which leads to little differences with what you expect.

x1 * 1/2 + x2 * 1/4 + x3 * 1/8 + ....

Even something as simple as 1/10 (0.1) cannot be stored exactly because it has a repetitive part:

1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + ...

Which is in binary 0.000110011001100110011001100...

Somewhere, this gets rounded of course which leads to little differences with what you expect.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

vivien siu

Ranch Hand

Posts: 143