How to control the number of significant digits for %g?

The %g format specifier causes Formatter to use either %f or %e, whichever is shorter.

When using %g, the precision determines the number of significant digits. The default precision is 6.

import java.util.Formatter;
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%g
“, 123.1234567);
// Format 4 decimal places.
fmt.format(”%.4g", 123.1234567);
System.out.println(fmt);
}
}
The output:
123.123
123.1