TheBritishAreComing
TheBritishAreComing

Reputation: 1727

Formatting a Double into the correct format

Having problem getting a Longitude/Latitude into the correct format for an api we don't have access to.

We need to convert this to a string and at the moment we have: 4.30044549E7 as the raw form but we need to convert it into 4.30044549 or similar (without Scientific Notation)

If we use

NumberFormat f = new DecimalFormat("#.#######");
f.format(4.30044549E7);

we get: 43004454.9

if we use

Double.toString(4.30044549E7);

we get: "4.30044549E7"

if we try to convert to an int we also get 43004454.9

Can anyone help? I can't find an acceptable solution to get rid of scientific notation on the number

Upvotes: 0

Views: 2124

Answers (3)

Peter
Peter

Reputation: 5798

you never can "format" 4.30044549E7 into 4.30044549 because they are not the same.

4.30044549E7 is in fact 43004454.9 so wanting a formatter to display that double as 4.30044549 is an odd question

Upvotes: 1

dee-see
dee-see

Reputation: 24088

If you really asked what I think you did, you could simply divide the number by 10 until you reach the format you wanted.

double value = 4.30044549E7;
while(value > 10 || value < -10){
  value /= 10;
}

System.out.println(String.format("%.8f", value)); //4.30044549

Upvotes: 3

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

Have you tried format?

String.format("%.10f",doubleValue); 

It will format your number with 10 digits after dot.

1e-5 -> 0.000010000
1e5  -> 100000.0000000000

Upvotes: 2

Related Questions