The simple answer is "you can't". A slightly longer answer is...

A "double" value is stored in binary. The idea of trailing decimal zeros, as in 106.4000, is meaningless in a "double" value.

A "double" value is also floating-point. This means it cannot store all decimal values (whole or fractions) with perfect accuracy. If you don't know about this stuff, Google for "floating point".

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

So you cannot keep the preceding zeros before the decimal and trailing zeros after the decimal.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41084

29

posted

0

Try this, and see what BigDecimal makes of leading and trailing zeroes:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41084

29

posted

0

Originally posted by Santhosh Kumar: Mathematically,

So you cannot keep the preceding zeros before the decimal and trailing zeros after the decimal.

Actually, I agree with the behaviour of BigDecimal that 1.1 is not exactly equivalent to 1.1000. Change the last statement of my BigDecimalTest class which I posted a few minutes back to read like this, and try again.

Originally posted by Campbell Ritchie: Actually, I agree with the behaviour of BigDecimal that 1.1 is not exactly equivalent to 1.1000.

But mathematically, it is, isn't it?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41084

29

posted

0

We're not here to do maths . . . BigDecimal does exactly what the original poster wants, retain the value and the original precision.

Originally posted by Campbell Ritchie: We're not here to do maths . . . BigDecimal does exactly what the original poster wants, retain the value and the original precision.

Well, it really has nothing to do with precision but rather with presentation (they're not the same). If the OP wants to have 4 leading numbers then the obvious choice would be to use java.text.DecimalFormat with the pattern: "0.0000":

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112

posted

0

Originally posted by Campbell Ritchie: We're not here to do maths . . .

It sounded to me like you were implying that the behavior of primitive doubles was somehow not fully correct. To that I would answer that it shows exactly the behavior a mathematician would expect.

I agree that if you need different behavior, you should use something different, and if that's BigDecimal in this case, fine.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41084

29

posted

0

Originally posted by Ilja Preuss: It sounded to me like you were implying that the behavior of primitive doubles was somehow not fully correct. . .

More likely, expressing myself awkwardly. Sorry. We seem all to agree in the end however.

Yes, and no. 1.1 equals 1.100, but 1.100 has better precision than 1.1. This can be illustrated by this example: 1.100 rounded to two digits is 1.1, but so is 1.109, 1.14 etc. So 1.1 really is only precise to say that it is a number N 1.15<N<=1.05, while with 1.100 is 1.150<N<=1.050.

But of course, this doesn't matter much to a computer.