Java currency formatter: Force to format using currency symbol -


I use the Spring Currency Format to format the value based on the currency code

  public string format (number number, string currency code) {CurrencyFormatter formatter = new CurrencyFormatter (); Formatter.setCurrency (Currency.getInstance (CURRENCYCODE)); Return formatter.print (number, locale .get default ()); }   

So if I say this as a format (10, "GBP"), then I want the value back as £ 10.00, regardless of location

Is this possible?

Whenever you display a currency, you need two pieces of information: currency code, and the location of which For example, when you display the USD in en_US locale, you want to show that $, but in en_AU locale, you give it US $ (because the currency of Australia is "dollar" Also called) and want to use the $ symbol Are AUD).

The issue you raised is that when you display the currency in your "non-primary" locale, the stock stinks the Java currency format, that is, everybody pounds much more in the US and The Euro is showing, but will look at one side, but the Java Library shows "GBP" and "Euro" while showing those currencies in the NII locale.

I got around with the following part of the code. I essentially specify my own symbols to use while displaying international currencies: public static numeric format newCurrencyFormat (currency currency, locale display local) {NumberFormat retVal = NumberFormat.getCurrencyInstance (displayLocale); RetVal.setCurrency (currency); // The default JDK handles the situation well when the currency is the default currency for the locale (currency.question (display local)) {Return Rated; } // Otherwise, we need to "fix things" while displaying non-country currency (if copy of the decimal time is example) {decimalf format decimal = (decimalf) retVal; String right I18NSymbol = getCorrectedInternationalCurrencySymbol (currency, displayLocale); If (correctedI18NSymbol! = Null) {decimal symbols dfs = decimal format.getDecimalFormatSymbols (); // It returns a clone of DFS dfs.setInternationalCurrencySymbol (correctedI18NSymbol); Dfs.setCurrencySymbol (correctedI18NSymbol); DecimalFormat.setDecimalFormatSymbols (DFS); }} Return Rate Vals; } Private Static Strings Reserve International Crimelobal (Currency Currency, Local DisplayPlot) {Resourcebundle IILNSSumblesRsourceBundle = ResourceBundleGetBundle ("right ILINCRMCsmbbs", displaylocale); If (i18nSymbolsResourceBundle.containsKey (currency.getCurrencyCode ()) {return i18nSymbolsResourceBundle.getString (currency.getCurrencyCode ()); } Else {return currency.getCurrencyCode (); }}

Then I have my properties file (right I18nCurrencySymbols.properties) where I specify the currency symbols to use:

  # Note that these currency symbols are used for the specified code, when the currency home is displayed in a different location from Locale # This file can be edited as needed, in addition, if in some cases a specific locale # here Listed standard end Will use a different symbol than the national one, then an additional properties file # can be added using the standard Risorsbndl loading algorithm. For example, if we have decided that we should go to the U.S. Dollar to the U.S. In the UK, when we want to show US dollar instead of dollar, IIT = $ ARS = $ AR AUD = AU $ BOB = $ AR file with i18nCurrencySymbols_en_GB.properties # $ B BRL = R $ CAD = CN $ CLP = CH $ COP = CL $ CRC = \ u20 A1 HRK = Mill CZ = K \ u010D DOP = RD $ xcd = EC $ euro = \ u20c ttq = q gid = g $ hnl = l HKD = HK $ HUF = FT INR = \ u20B9 IDR = RP ILS = \ u20AA JMD = J $ JPY = JP \ u00A5 KRW = \ u20A 9 NZD = NZ $ NIO = C $ PAB = B / PYG = GS Pen = S / PHP = \ u20B1 PLN = \ u007A \ u0142 RON = LEI SGD = S $ ZAR = R TWD = NT $ THB = \ u0E3F TTD = TT $ GBP = \ U00A3 USD = US $ UUU = $ U WEF = BS VND = \ U20AB    

Comments