在 Java 中将 double 转换为 String,反之亦然,而不会失去准确性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/755449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a double to a String in Java and vice versa without losing accuracy
提问by Iain
A Stringrepresentation of a doubleis written to and read from a file by a C#application.
甲字符串一个的表示双写入和从由文件读C#应用。
The C# application converts the double to a string using the following fragment:
C# 应用程序使用以下片段将 double 转换为字符串:
value.ToString("R", NumberFormatInfo.InvariantInfo);
The C# application converts the string to a double using the following fragment
C# 应用程序使用以下片段将字符串转换为双精度值
double num = double.Parse(s, NumberStyles.Float, (IFormatProvider) NumberFormatInfo.InvariantInfo);
If that same file were to be written to and read from by a Javaapplication, how would you go about converting the types without losing data?
如果Java应用程序要写入和读取同一个文件,您将如何在不丢失数据的情况下转换类型?
采纳答案by Jon Skeet
Just using Double.parseDouble()
and Double.toString()
should work without losing data, I believe. In particular, from the docs for Double.toString()
:
我相信,只是使用Double.parseDouble()
并且Double.toString()
应该可以在不丢失数据的情况下工作。特别是,来自以下文档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.
m 或 a 的小数部分必须打印多少位?必须至少有一个数字来表示小数部分,并且超过这个数字,但只有尽可能多的数字,以唯一地将参数值与双精度类型的相邻值区分开来。也就是说,假设 x 是由此方法为有限非零参数 d 生成的十进制表示所表示的精确数学值。那么 d 必须是最接近 x 的双精度值;或者如果两个 double 值同样接近 x,则 d 必须是其中之一,并且 d 的有效数的最低有效位必须为 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.
另一种选择,如果你想保留确切的字符串表示(这不是完全一样的东西)是BigDecimal
在 Java 中使用。
回答by Pooria
Simply you have to use Java Double wrapper class which is capital "D" "Double"
只需使用 Java Double 包装器类,即大写“D”“Double”
int String s = Double.toString(yourDoubleVariable);
int String s = Double.toString(yourDoubleVariable);
回答by willcodejavaforfood
From double to String:
从双到字符串:
String stringValue = Double.toString(value);
From String to double
从字符串到双精度
double doubleValue = Double.valueOf(value);
回答by Miquel
Doubles have a limited precision and might not preserve the string intact. The BigDecimal class has arbitrary precission and keeps sring representation.
双打精度有限,可能无法完整保存字符串。BigDecimal 类具有任意精度并保持 sring 表示。
To convert a string into a BigDecimal:
要将字符串转换为 BigDecimal:
BigDecimal d = new BigDecimal("10.1234567890");
To Convert a BigDecimal into string:
要将 BigDecimal 转换为字符串:
System.out.println(d.toString());
More details here: http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html
更多细节在这里:http: //epramono.blogspot.com/2005/01/double-vs-bigdecimal.html
回答by JeeBee
Do you need the string representation for any purpose, or it is merely for a textual data transport (e.g., SOAP/REST message)?
您是否出于任何目的需要字符串表示,或者它仅用于文本数据传输(例如,SOAP/REST 消息)?
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.
对于后者,您可以将 double 值转换为 long using java.lang.Double.doubleToRawLongBits(double value)
,然后再转换为 double using java.lang.Double.longBitsToDouble(long value)
。您可以将长值作为十六进制编码的字符串传输。
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)
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(长)
This will preserve the exact 64-bit double value that you have, but it won't be human readable (for most! ;) ).
这将保留您拥有的确切 64 位双精度值,但它不会是人类可读的(大多数情况下!;))。