Reputation: 35
What is the best way to format the following number that is given to me as a String?
String number = "1.574e10" //assume my value will always be a String
I want this to be a String with the value: 1000500000.57
How can I format it as such?
Upvotes: 0
Views: 3260
Reputation: 109577
double x = Double.valueOf("1.574e10");
String s = String.format("%.2f", x);
The f
specifier for floating point here gives two positions after the decimal point, with no scientific exponent (e10).
Upvotes: 1
Reputation: 417947
Parse a double
, then format the double
. BigDecimal.toString()
provides a canonical representation:
// Double.parseDouble() also accepts format like "1.574e10"
String formatted = new BigDecimal(Double.valueOf("1.574e10")).toString()
Result:
15740000000
About nicely formatting floating point numbers, see:
How to nicely format floating numbers to String without unnecessary decimal 0?
Upvotes: 0