How to print number with thousands separator in Java

1 Answer

0 votes
public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            System.out.printf( "%,d\n", 199);
            System.out.printf( "%,d\n", 1234);
            System.out.printf( "%,d\n", 100000000);
            System.out.printf( "%,d\n", -10000000);
            System.out.printf( "%,.2f\n", 12345.31 );
            System.out.printf( "%,.2f\n", 12345678.97 );
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }  
    }
}



 
/*
 
run:
 
199
1,234
100,000,000
-10,000,000
12,345.31
12,345,678.97
 
*/

 



answered Jun 9, 2015 by avibootz
edited Jul 25, 2022 by avibootz

Related questions

1 answer 191 views
2 answers 1,126 views
1 answer 166 views
1 answer 221 views
1 answer 127 views
1 answer 138 views
1 answer 124 views
...