m n :
I have searched the internet but I could not find any solution (maybe, I've searched badly).
I want to convert the String
"108595000.5"
to a double
and I have used these methods:
Double.parseDouble("108595000.5");
Double.valueOf("108595000.5");
Unfortunately, both of them return 1.08595E8
.
How can I convert this String
to double
without problem?
deHaar :
The methods you have used do not return 1.08595E8
, instead, they return the number and what you are complaining about is the representation of that number in the console (or as a String
).
However, you can specify how to output a double
yourself with a specified formatting, see this example:
public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
double: 1.085950005E8
engineering: 108595000.5
plain: 108595000.5
rounded with fix decimal places: 108595000.50
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=349290&siteId=1