问题
I am to convert a double value to a String.
double d=12345678.999;
String str = String.valueOf(d);
or, just plain
String str = ""+d;
Does it, however in the exponent form-- returning the String value:
1.2345678999E7
I need the value as is in the String:
12345678.999
Is there a way to do this? I can parse the string for the value of exponent-- the portion after 'E' and go from there, but there must be an easier way of doing this.
//===========================
EDIT:
the length of the decimal part in the input isn't known, it can be any number of digits:
12345678.999
or
12345678.9999999
Imposing a format for the decimal part would fix the number of decimal digits(?)
回答1:
You should use NumberFormat to format Strings so that they do not show up in scientific notation.
String str = NumberFormat.getInstance().format(d);
回答2:
use String.format()
:
double d=12345678.999;
System.out.println(String.format("%12.3f", d));
来源:https://stackoverflow.com/questions/25067808/convert-double-to-string-in-java