I need to convert some floats to format like this: 2,5000E-003
, 2,8625E+000
I know i can do it like this:
String.format("%s,%sE%s", digitInt, digitAfterDot, digitDegree)
But I hope somebody knows more clever and natty solution
You can use:
String.format("%1.4e",12345.67890123)
The result of this is:
1,2346e+04
Here is some documentation
EDIT
If you need 3 digits in the exponent, you can use the DecimalFormat
Formatter:
DecimalFormat format = new DecimalFormat("0.0000E000");
System.out.print(format.format(12345.67890123f));
Which outputs:
1,2346E004
Note that it does not come with a positive sign if the exponent is positive. You can fix this with:
DecimalFormat format = new DecimalFormat("0.0000E000");
String result = format.format(12345.67890123f);
if (!result.contains("E-")) {
result = result.replace("E", "E+");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.