Iain
Iain

Reputation: 10749

Convert a double to a String in Java and vice versa without losing accuracy

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 losing data?

Upvotes: 3

Views: 30709

Answers (5)

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

From double to String:

String stringValue = Double.toString(value);

From String to double

double doubleValue = Double.valueOf(value);

Upvotes: 0

Miquel
Miquel

Reputation: 4829

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

Upvotes: 6

JeeBee
JeeBee

Reputation: 17546

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! ;) ).

Upvotes: 6

Pooria
Pooria

Reputation: 780

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

int String s = Double.toString(yourDoubleVariable);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500055

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.

Upvotes: 13

Related Questions