aspose file tools*
The moose likes Java in General and the fly likes How to covert a String value to double without loosing the precision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to covert a String value to double without loosing the precision" Watch "How to covert a String value to double without loosing the precision" New topic
Author

How to covert a String value to double without loosing the precision

Ponery Nishad
Greenhorn

Joined: May 08, 2008
Posts: 5
Hi,

How can I convert a String value "106.4000" to a double, without loosing the precisions after 4 ?

If I use,

double dbl = Double.parseDouble("106.4000");

I am getting 106.4 only.

But I need the exact string value as double.


So please help me to do this.

Thanks in advance...

regards,
Nishad P
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch

Your problem sounds like a job for . . . BigDecimal!
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Mathematically,



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

Joined: Oct 13, 2005
Posts: 39436
    
  28
Try this, and see what BigDecimal makes of leading and trailing zeroes:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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: 39436
    
  28
We're not here to do maths . . . BigDecimal does exactly what the original poster wants, retain the value and the original precision.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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
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: 39436
    
  28
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
Originally posted by Ilja Preuss:


But mathematically, it is, isn't it?

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.
 
 
subject: How to covert a String value to double without loosing the precision