Close

Java String Formatting - How to apply precision with floating point in scientific notation using String#printf()?

Java String Formatting Java 

This example shows how to use precision with floating-point in scientific notation with String#printf()

Syntax: x.ye or x.yE; where x= padding (width) and y= precision.

This is used for controlling the length of the decimal points.

package com.logicbig.example.string;

public class StringPrintfScientificPrecision {

public static void main(String[] args) {
System.out.printf("%1.2e%n", 987.65);
System.out.printf("[%10.2e]%n", 987.65);
System.out.printf("[%-10.1e]%n", 987.65);
System.out.printf("%5.2E%n", 987.65);
}
}

Output

9.88e+02
[ 9.88e+02]
[9.9e+02 ]
9.88E+02




See Also