Java - Format floating-point number with width and precision

Description

Format floating-point number with width and precision

Demo

public class Main {
  public static void main(String[] args) {
    System.out.printf("%.2e %n", 2028.27);
    System.out.printf("%.2f %n", 2028.27);
    System.out.printf("%.2g %n", 2028.27);

    System.out.printf("'%8.2e' %n", 2028.27);
    System.out.printf("'%8.2f' %n", 2028.27);
    System.out.printf("'%8.2g' %n", 2028.27);

    System.out.printf("'%10.2e' %n", 2028.27);
    System.out.printf("'%10.2f' %n", 2028.27);
    System.out.printf("'%10.2g' %n", 2028.27);

    System.out.printf("'%-10.2e' %n", 2028.27);
    System.out.printf("'%-10.2f' %n", 2028.27);
    System.out.printf("'%-10.2g' %n", 2028.27);

    System.out.printf("'%010.2e' %n", 2028.27);
    System.out.printf("'%010.2f' %n", 2028.27);
    System.out.printf("'%010.2g' %n", 2028.27);
  }//from  w w  w  .j a  v  a 2s . co  m
}

Result

Related Topic