Display Numbers with Thousands Separator in Java



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
Updated on: 2020-06-27T05:41:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements