Formatting Currencies

double currency = 567123678.99;
											NumberFormat currencyFormatter;
											String formattedCurrency;
											currencyFormatter =
											NumberFormat.getCurrencyInstance();
											formattedCurrency =
											currencyFormatter.format(currency);
											System.out.format("%s%n", formattedCurrency);

As with the previous phrase on formatting numbers, here we make use of the NumberFormat class, but this time to format a currency value. We use the getCurrencyInstance() of the NumberFormat class to get a currency formatting instance of the class. Using this instance, we can pass a floating point value and it will return a formatted currency value. The output of the phrase shown will be the following string:

$567,123,678.99

In addition to placing commas in the appropriate places, ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.