Display numbers with thousands separator in Java

Java 8Object Oriented ProgrammingProgramming

To display number with thousands separator, set a comma flag.

System.out.printf( "%,d\n",78567);

The above would result.

78, 567

Let’s check for bigger numbers.

System.out.printf( "%,d\n", 463758);

The above would result.

463,758

Example

 Live Demo

public class Demo {
   public static void main( String args[] ) {
      System.out.printf( "%,d\n", 95647 );
      System.out.printf( "%,d\n", 687467 );
      System.out.printf( "%,.2f\n", 7546.21 );
      System.out.printf( "%,.2f\n", 463758.787 );
      System.out.printf( "%,.2f", 123456.5 );
   }
}

Output

95,647
687,467
7,546.21
463,758.79
123,456.50
raja
Published on 14-Dec-2018 10:47:42
Advertisements