views:

4124

answers:

5

A String representation of a double is written to and read from a file by a C# application.

The C# application converts the double to a string using the following fragment:

value.ToString("R", NumberFormatInfo.InvariantInfo);

The C# application converts the string to a double using the following fragment

double num = double.Parse(s, NumberStyles.Float, (IFormatProvider) NumberFormatInfo.InvariantInfo);

If that same file were to be written to and read from by a Java application, how would you go about converting the types without loosing data?

+6  A: 

Just using Double.parseDouble() and Double.toString() should work without losing data, I believe. In particular, from the docs for Double.toString():

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.

Another alternative, if you want to preserve the exact string representation (which isn't quite the same thing) is to use BigDecimal in Java.

Jon Skeet
Downvoters - please leave comments, otherwise the downvote serves little purpose.
Jon Skeet
I suspect because (like myself) they didn't fully realize the significance of your quoted text. At face value, it seems obvious that Math.PI would be more accurate than Double.parseDouble(Double.toString(Math.PI)), but your quoted code comment seems to say that nothing is lost. I'll give you a +1.
Gunslinger47
System.out.println(Math.PI == Double.parseDouble(Double.toString(Math.PI))); // prints "true"
Gunslinger47
A: 

Hi,

Simply you have to use Java Double wrapper class which is capital "D" "Double"

int String s = Double.toString(yourDoubleVariable);

Pooria
A: 

From double to String:

String stringValue = Double.toString(value);

From String to double

double doubleValue = Double.valueOf(value);
willcodejavaforfood
your second answer is wrong, it will return String.
Bhushan
@Bhushan - Cheers mate, corrected it :)
willcodejavaforfood
+5  A: 

Doubles have a limited precision and might not preserve the string intact. The BigDecimal class has arbitrary precission and keeps sring representation.

To convert a string into a BigDecimal:

BigDecimal d = new BigDecimal("10.1234567890");

To Convert a BigDecimal into string:

System.out.println(d.toString());

More details here: http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html

Miquel
Bear in mind that the original value was from a C# double, which has the same range and precision.
Jon Skeet
From the question I understand that if the same operation was going to be done in Java. My point is that you need to use BigDecimal if you don't want to lose precision in operations.
Miquel
I understood the question to be trying to accurately read a value which was previously a double in C#, then stored in a text file. You don't need to use BigDecimal for that.
Jon Skeet
I agree with you if that is the question interpretation
Miquel
How do you interpret the question in any other way? Where is there any mention of any operation other than converting between double to string and back again?
Jon Skeet
+3  A: 

Do you need the string representation for any purpose, or it is merely for a textual data transport (e.g., SOAP/REST message)?

For the latter, you can convert the double value into a long using java.lang.Double.doubleToRawLongBits(double value) and back into a double using java.lang.Double.longBitsToDouble(long value). You can transport the long value as a hex-encoded string.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#doubleToRawLongBits(double) http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#longBitsToDouble(long)

This will preserve the exact 64-bit double value that you have, but it won't be human readable (for most! ;) ).

JeeBee