DecimalFormat Class

The java.text.DecimalFormat class is used for formatting numbers as per customized format and as per locale.

Example – Format Numbers

In this example, we’re formatting numbers based on a given pattern.

Open Compiler

importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="####,####.##";double number =123456789.123;DecimalFormat numberFormat =newDecimalFormat(pattern);System.out.println(number);System.out.println(numberFormat.format(number));}}

Output

It will print the following result.

1.23456789123E8
1,2345,6789.12

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *