Reputation: 323
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 :)
Upvotes: 0
Views: 156
Reputation: 4574
You can achieve what you want with the standard java.text.DecimalFormat
import java.text.DecimalFormat;
...
Double value = Double.parseDouble("0.00000012");
DecimalFormat formatter = new DecimalFormat("0.#####E0");
String formatted = formatter.format(value);
System.out.println(formatted);
This prints out : 1.2E-7
Upvotes: 0
Reputation: 4574
With your GWT NumberFormat, can you try the below and see if it works ? (eg using BigDecimal instead of Double)
System.out.println(NumberFormat.getScientificFormat().format(new BigDecimal("0.00000012")));
Upvotes: 0
Reputation: 18346
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);
Upvotes: 2