Dijaska skupnost SGV
Dijaska skupnost SGV

Reputation: 35

Formatting troubles: How to power to number?

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

Answers (2)

Joop Eggen
Joop Eggen

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

icza
icza

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

Related Questions