Formatting a number is pretty simple. We use the DecimalFormat class to specify a custom format:
NumberFormat formatter1 = new DecimalFormat("#,###"); System.out.println(formatter1.format(123123)) // displays 123,123 NumberFormat formatter2 = new DecimalFormat("0.00"); System.out.println(formatter2.format(123123)) // displays 123123.00 NumberFormat formatter3 = new DecimalFormat("#,##0.00"); System.out.println(formatter3.format(123123.123)) // displays 123,123.12
There are a lot of patterns you could use to format numbers.
Just take a look at http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
No comments:
Post a Comment