user2982112
user2982112

Reputation: 17

Decimal Format conversion issue

I'm using DecimalFormat in order to convert double to string like this:

DecimalFormat df = new DecimalFormat("###.###");

df.format(km);

The problem is, although the variable km has the value of 9.655195710258606E-5, the result of the df.format method returns 0 as string.But I expect something like 9.655 instead of 0.

Any help & suggestions will be appreciated

Thank you for your concern

Upvotes: 0

Views: 166

Answers (2)

Henry
Henry

Reputation: 43738

It works as it should. The string "9.655" would be wrong. The value is actually 0.00009655 and if you round it to 3 decimal places you get "0".

To see some more digits, you can use more decimal places in the format #.###### or force scientific notation with a format like this #.###E0.

Upvotes: 3

Gil Moshayof
Gil Moshayof

Reputation: 16761

In the end of these numbers, the E(x) represents the exponent. E-5, means the exponent is -5, which means the actual number is 9.655 * (1/(10^5)).

Upvotes: 0

Related Questions