<<< Comparing BigDecimals | Index | The NumberFormat class >>> |
When dealing with money, it would be nice to have the amounts held by BigDecimal properly formatted.
For example, when displaying US currency you might want to include a dollar sign and a comma as a thousands separator.
The NumberFormat class, found in the java.text library, can create an appropriate object for US currency with the following code:
import java.text.NumberFormat; import java.util.Locale; BigDecimal payment = new BigDecimal("1000000.00")); NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); double doublePayment = payment.doubleValue(); String strPayment = nf.format(doublePayment); System.out.println(strPayment);
<<< Comparing BigDecimals | Index | The NumberFormat class >>> |