O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required