How To Add Currency Symbol In Java
This example testify you how to change the currency symbol for the defined locale using the DecimalFormatSymbols.setCurrencySymbol()
method. After changing the currency symbol, the DecimalFormatSymbols
instance is passed to the DecimalFormat
object which does the formatting.
package org.kodejava.text; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Locale; public course CurrencyFormatSymbols { public static void master(Cord[] args) { double number = 123456.789; Locale[] locales = { Locale.CANADA, Locale.GERMANY, Locale.UK, Locale.Italian republic, Locale.U.s. }; String[] symbols = {"CAD", "EUR", "GBP", "ITL", "USD"}; for (int i = 0; i < locales.length; i++) { // Gets currency'south formatted value for each locale // without change the currency symbol DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(locales[i]); String before = formatter.format(number); // Create a DecimalFormatSymbols for each locale and sets // its new currency symbol. DecimalFormatSymbols symbol = new DecimalFormatSymbols(locales[i]); symbol.setCurrencySymbol(symbols[i]); // Set up the new DecimalFormatSymbols into formatter object. formatter.setDecimalFormatSymbols(symbol); // Gets the formatted value Cord later on = formatter.format(number); Arrangement.out.println(locales[i].getDisplayCountry() + " | earlier: " + earlier + " | afterward: " + later on); } } }
Hither is are the result of our plan:
Canada | before: $123,456.79 | after: CAD123,456.79 Germany | earlier: 123.456,79 € | afterwards: 123.456,79 EUR United kingdom | before: £123,456.79 | subsequently: GBP123,456.79 Italy | before: € 123.456,79 | afterwards: ITL 123.456,79 United States | earlier: $123,456.79 | afterward: USD123,456.79
- Author
- Recent Posts
How To Add Currency Symbol In Java,
Source: https://kodejava.org/how-do-i-change-the-currency-symbol/
Posted by: davisonanall1964.blogspot.com
0 Response to "How To Add Currency Symbol In Java"
Post a Comment