1

How do I convert scientific notation to regular floating value. For example for the value 1.255555555569E10, I want 12555555555.69 as output.

I have tried the following way

Double val = new BigDecimal(1.25E10).doubleValue();
System.out.println(val);
// output : 1.23E10

I tried using long aswell

long val = new BigDecimal(1.25E10).longValueExact();
System.out.println(val);

// output is 12500000000

But long fails if the value has decimal like

long val = new BigDecimal(1.255555555569E10).longValueExact();
System.out.println(val);

// output is 12555555555

instead

// output should be  12555555555.69

please suggest.

origin
  • 120
  • 2
  • 8
  • 1
    "This is because the value is exceeding the range of Double" sure not! Otherwise `doubleValue()` would have returned Infinity (not to speak about [the double] `1.25E10`) – user85421 Sep 12 '19 at 11:39

1 Answers1

2

You could take a look at DecimalFormat:

String pattern = "###,##0.00";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
System.out.println(decimalFormat.format(1.25E10));

Sources:

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
  • 2
    String val = new BigDecimal(1.23E7).toPlainString(); System.out.println(val); //output : 12555555555.6900005340576171875.This has also given me exact output. – origin Sep 12 '19 at 11:45