I found it a bit confusing figuring out how to format numbers and currency etc in Java, so I thought I would write a quick blog about it.  Lets consider a big number such as:

123,123,123,123.99

There are a number of things we may want to do this number.  The first being getting rid of the decimals.  So the first key thing to remember is convert your numbers to BigDecimals so there is far more flexibility with using the number:

BigDecimal bd = new BigDecimal(123,123,123,123.99);

Great, now lets write some code that will remove the commas:

BigDecimal bd = new BigDecimal(123,123,123,123.99);
NumberFormat nf = DecimalFormat.getInstance();
if (nf instanceof DecimalFormat) {
((DecimalFormat) nf).setGroupingSize(0);
}

With this approach, you can take advantage of the DecimalFormat class and to other things such as change the appearance of a negative number to have ( … ) instead of minus (-) sign:

BigDecimal bd = new BigDecimal(123,123,123,123.99);
NumberFormat nf = DecimalFormat.getInstance();
if (nf instanceof DecimalFormat) {
((DecimalFormat) nf).setGroupingSize(0);
((DecimalFormat) nf).setNegativePrefix("(");
((DecimalFormat) nf).setNegativeSuffix(")");
}

Simple carry on with this approach to format a few other things.  Consider using the CurrencyFormat class when dealing with money but follow this simple framework.