I want to format this String
:
String number = "3.4213946120686956E-9";
to this:
String number = "3.42E-9";
Rounding to 2 decimals. How can I achieve it?
I want to format this String
:
String number = "3.4213946120686956E-9";
to this:
String number = "3.42E-9";
Rounding to 2 decimals. How can I achieve it?
Read about DecimalFormat.
This is not the most elegant solution but you could:
EX: String number = "3.4213946120686956E-9";
if the String has a period and an E in it
split the string by the period, and get the first 2 characters after the period = 42
get the last 3 characters at the end = E-9
and then just add it back together with all the characters before the period = 3 + "." + 42 + E-9
Try
new DecimalFormat("0.##E0")
. Javadoc: DecimalFormatExample:
Output: 3.42E-9