How to convert a string 3.0103E-7 to 0.00000030103 in Java?

12,367

Solution 1

I would use BigDecimal.Pass your string into it as a parameter and then use String.format to represent your newly created BigDecimal without scientific notation. Float or Double classes can be used too.

Solution 2

Apparently the correct answer is to user BigDecimal and retrieve the precision and scale numbers. Then use those numbers in the Formatter. Something similar like this:

BigDecimal bg = new BigDecimal(rs.getString(i));
Formatter fmt = new Formatter();
fmt.format("%." + bg.scale() + "f", bg);
buf.append( fmt);

Solution 3

Using BigDecimal:

public static String removeScientificNotation(String value)
{
    return new BigDecimal(value).toPlainString();
}

public static void main(String[] arguments) throws Exception
{
    System.out.println(removeScientificNotation("3.0103E-7"));
}

Prints:

0.00000030103

Solution 4

double d = Double.parseDouble("7.399999999999985E-5");
NumberFormat formatter = new DecimalFormat("###.#####");
String f = formatter.format(d);
System.out.println(f);    // output --> 0.00007
Share:
12,367
erotsppa
Author by

erotsppa

Updated on June 05, 2022

Comments

  • erotsppa
    erotsppa almost 2 years

    How to convert a string 0E-11 to 0.00000000000 in Java? I want to display the number in non scientific notations. I've tried looking at the number formatter in Java, however I need to specific the exact number of decimals I want but I will not always know. I simply want the number of decimal places as specificed by my original number.

  • Eugene Ryzhikov
    Eugene Ryzhikov over 14 years
    This is exactly what I suggested :)
  • Dave Jarvis
    Dave Jarvis over 14 years
    Yes, Eugene, but the devil is in the details; working example code is usually more helpful than a description. For example, someone suggested I use EXEC on a stored procedure (in Oracle 10g). The idea was correct (used a stored procedure instead of a function), but the details (CALL vs. EXEC) were wrong. As Torvalds once said, "Show me the code." lkml.org/lkml/2000/8/25/132
  • Eugene Ryzhikov
    Eugene Ryzhikov over 14 years
    Are you joking? What kind of details do you need here? It is very simple already... the whole deal is about idea. :)
  • Christian Esperar
    Christian Esperar about 7 years
    Apparently, this is the only fix my issue!
  • strobering
    strobering over 4 years
    Great! This is the easiest form. Shoud be the best answer