jersam515
jersam515

Reputation: 657

How can I convert a double to a string without notation and with a high amount of accuracy in Java?

I'm trying to convert a double to a string without notation, and tried this:

f= Double.valueOf(c.getString(c.getColumnIndex(NotesDbAdapter.KEY_VALUE)));  
NumberFormat formatter = new DecimalFormat("###.##############");  

However, the value of 7^3^7 is returning as: 558546000000000000 opposed to 558545864083284007. As always help would be greatly appreciated.

Upvotes: 3

Views: 211

Answers (2)

Jim Tough
Jim Tough

Reputation: 15210

If you already have the huge decimal number in string format, try using the BigDecimal class, something like this:

    BigDecimal bigDecimalValue = new BigDecimal("1234567890123456789012345678901234567890.54321");
    LOGGER.info("bigDecimalValue: {}", bigDecimalValue.toPlainString());

You should get back the original value with no precision loss:

bigDecimalValue: 1234567890123456789012345678901234567890.54321

Upvotes: 0

user207421
user207421

Reputation: 310875

  1. You already had the value as a String. Why convert it to double at all?

  2. You can't get precision out of a double that it cannot hold. 558545864083284007 has 18 decimal digits. A double has 53 bits of binary precision, which is about 15.9 decimal digits. Google for 'What every computer scientist should know about floating-point'.

  3. ###.############## is not a suitable formatting mask for 558545864083284007.

Upvotes: 4

Related Questions