Search code examples
javagwtnumber-formatting

[GWT][Java] Scientific notation auto rounded?


I need to use Scientific notation to represent a value. I use this code but the value is rounded and I don't want that...

import com.google.gwt.i18n.client.NumberFormat;
...
Double value = Double.parseDouble("0.00000012");
String formatted = NumberFormat.getScientificFormat().format(value);
System.out.println(formatted);

The result is:

1E-7

and not

1.2E-7

Could you help me? Thanks :)


Solution

  • The same format pattern used in funkyjelly's answer should work with GWT's NumberFormat type:

    import com.google.gwt.i18n.client.NumberFormat;
    ...
    Double value = Double.parseDouble("0.00000012");
    String formatted = NumberFormat.getFormat("0.#####E0").format(value);
    System.out.println(formatted);