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, the currency formatter also automatically adds the dollar sign character preceding the string.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net