2017-05-23 84 views
2

我試圖使用正確的格式設置規則格式化數據,給定ISO 4217 數字貨幣代碼。從ISO **數字**貨幣代碼查找CurrencyUnit

例如,給定數量爲99.99和數字代碼840,我希望產生一個字符串'USD99.99'或類似的字符串。

看看JSR 354的實現,似乎我需要先找到合適的貨幣單位。但是,我似乎無法爲此組成有效的CurrencyQuery。

鑑於

CurrencyQuery query = CurrencyQueryBuilder.of().setNumericCodes(840).build(); 
CurrencyUnit cu = Monetary.getCurrency(query); 
MonetaryAmount amount = FastMoney.of(99.99, cu); 
System.out.println(MonetaryFormats.getAmountFormat(Locale.ENGLISH).format(amount)); 

我收到以下異常:

Exception in thread "main" javax.money.MonetaryException: Ambiguous request for CurrencyUnit: CurrencyQuery (
{Query.numericCodes=[840]}), found: [BIF, ARS, DZD, COP, CVE, CHF, HRK, IQD, LAK, XPF, DKK, BAM, ZMK, ITL, XBB, XUA, XPT, BRL, CAD, NZD, SEK, USN, XTS, IEP, SRG, STD, BHD, USS, XAF, AUD, CDF, MTL, QAR, AOA, MGA, TOP, LYD, BOV, ISK, ADP, HTG, IDR, ANG, XXX, BTN, KHR, LTL, KES, INR, MGF, OMR, BEF, EUR, BOB, JPY, XBA, AED, SOS, XPD, SKK, FKP, XBC, CRC, SCR, MYR, MZN, LVL, GYD, XDR, GHS, XCD, MDL, MRO, ZWN, LSL, PEN, ATS, XOF, AZM, BYB, TRY, SRD, TTD, IRR, NOK, BZD, TRL, BDT, KZT, CLF, SLL, USD, ETB, BSD, SDD, UYU, ESP, VEB, TMM, CUP, NAD, DOP, NPR, TJS, VND, ZWR, AMD, FRF, TWD, LUF, GNF, MOP, MKD, JMD, CZK, PTE, LBP, ZWD, RSD, FIM, AZN, SDG, GHC, THB, MMK, BBD, PLN, ROL, NGN, RON, TZS, ERN, GBP, TPE, UZS, XBD, SGD, MXV, SVC, FJD, GWP, CNY, BYR, RUB, CSD, XAG, HKD, BND, SHP, KPW, CLP, KYD, DJF, ILS, RUR, LRD, SYP, SAR, ZWL, VUV, DEM, XSU, SIT, PHP, PYG, TND, HNL, ZAR, XFO, BMD, MWK, ZMW, UGX, NLG, JOD, KWD, YER, GMD, CUC, BGL, RWF, UAH, MVR, AWG, MZM, LKR, CYP, VEF, BWP, MXN, KRW, TMT, GIP, MUR, MNT, EEK, GTQ, PKR, PAB, KGS, GRD, YUM, SZL, GEL, WST, MAD, EGP, SBD, ALL, PGK, NIO, AFA, BGN, XAU, SSP, AFN, HUF, XFU, AYM, KMF] 

我應該如何撰寫貨幣查詢?有沒有更好的辦法?

TestCurrencyQuery.java

package test; 

import java.util.Locale; 

import javax.money.CurrencyQuery; 
import javax.money.CurrencyQueryBuilder; 
import javax.money.CurrencyUnit; 
import javax.money.Monetary; 
import javax.money.MonetaryAmount; 
import javax.money.format.MonetaryFormats; 

import org.javamoney.moneta.FastMoney; 

public class TestCurrencyQuery { 
    public static void main(String[] args) { 
    System.out.println(System.getProperty("java.vm.name")); 
    System.out.println(System.getProperty("java.version")); 
    System.out.println(System.getProperty("java.specification.vendor")); 

    CurrencyQuery query = CurrencyQueryBuilder.of().setNumericCodes(840).build(); 
    CurrencyUnit cu = Monetary.getCurrency(query); 
    MonetaryAmount amount = FastMoney.of(99.99, cu); 
    System.out.println(MonetaryFormats.getAmountFormat(Locale.ENGLISH).format(amount)); 
    } 
} 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>test</groupId> 
    <artifactId>test-jsr354</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.javamoney</groupId> 
     <artifactId>moneta</artifactId> 
     <version>1.1</version> 
    </dependency> 
    </dependencies> 
</project> 

控制檯輸出

Java HotSpot(TM) 64-Bit Server VM 
1.8.0_31 
Oracle Corporation 
Exception in thread "main" javax.money.MonetaryException: Ambiguous request for CurrencyUnit: CurrencyQuery (
{Query.numericCodes=[840]}), found: [KHR, RON, XTS, FJD, LRD, MVR, BRL, ZMK, PKR, LBP, LYD, SDG, MXV, AED, XDR, STD, CDF, QAR, ISK, SHP, CSD, FRF, BMD, PTE, PLN, XUA, UYU, GYD, LAK, ZAR, ZMW, MKD, XSU, XAF, EEK, ILS, UGX, ITL, XPT, ROL, MUR, INR, TWD, KES, FIM, CHF, HNL, ARS, BGL, KRW, SBD, PHP, RUR, PYG, SRD, AWG, MXN, MGA, SGD, TRL, MZN, SOS, MOP, XBA, IRR, CZK, XFU, TRY, UZS, XFO, TTD, SDD, SCR, COP, ALL, XAU, SYP, USN, PAB, CUC, JMD, GNF, BHD, ANG, IDR, GWP, DEM, SLL, SVC, HRK, NLG, TMT, KZT, MTL, CUP, VEF, ZWN, NOK, LVL, CLF, TMM, BEF, VEB, MZM, DOP, GHS, BBD, XPF, CYP, UAH, BAM, TPE, BND, BYB, MGF, SIT, BWP, XBD, XCD, LSL, AYM, RUB, ADP, SEK, YUM, AOA, WST, GBP, ZWD, THB, ZWL, ESP, MAD, USS, ETB, NGN, AUD, TZS, XAG, VND, BTN, JOD, PGK, XBB, DKK, VUV, BYR, NZD, EGP, SSP, KMF, CNY, MWK, AZM, SAR, CVE, HUF, HTG, SRG, AZN, GIP, LUF, DJF, GEL, RWF, TJS, TOP, TND, ERN, OMR, GRD, MMK, LKR, FKP, NIO, ATS, KPW, PEN, AMD, IEP, BZD, XXX, BIF, GTQ, KYD, EUR, LTL, CRC, HKD, BGN, RSD, SZL, USD, KWD, CLP, MYR, BOB, AFA, GHC, JPY, AFN, GMD, MDL, IQD, BSD, MNT, NPR, MRO, XPD, DZD, NAD, CAD, BOV, KGS, ZWR, SKK, XOF, YER, BDT, XBC] 
    at javax.money.spi.MonetaryCurrenciesSingletonSpi.getCurrency(MonetaryCurrenciesSingletonSpi.java:173) 
    at javax.money.Monetary.getCurrency(Monetary.java:506) 
    at test.TestCurrencyQuery.main(TestCurrencyQuery.java:21) 
+0

你可以分享你使用的配置文件(maven,gradle)/ jar嗎? –

+0

編輯問題與請求的細節 –

回答

0

如前所述,您無法使用此實現方式通過貨幣代碼訪問CurrencyUnit。 如果您的任務嚴格接受貨幣代碼作爲參數,您可以創建自己的映射。更好的方法是使用數據庫,您可以在其中存儲數字代碼和貨幣代碼之間的映射。

package test; 

import java.util.Locale; 

import javax.money.CurrencyQuery; 
import javax.money.CurrencyQueryBuilder; 
import javax.money.CurrencyUnit; 
import javax.money.Monetary; 
import javax.money.MonetaryAmount; 
import javax.money.format.MonetaryFormats; 

import org.javamoney.moneta.FastMoney; 

public class TestCurrencyQuery { 
    public static void main(String[] args) { 
    System.out.println(System.getProperty("java.vm.name")); 
    System.out.println(System.getProperty("java.version")); 
    System.out.println(System.getProperty("java.specification.vendor")); 

    CurrencyQuery query = CurrencyQueryBuilder.of().setCurrencyCodes(getCurrencyCodesFromNumeric(840)).build(); 
    CurrencyUnit cu = Monetary.getCurrency(query); 
    MonetaryAmount amount = FastMoney.of(99.99, cu); 
    System.out.println(MonetaryFormats.getAmountFormat(Locale.ENGLISH).format(amount)); 
    } 

    public String[] getCurrencyCodesFromNumeric(int... codes) { 
     Collection<String> currencyCodes = // query db and return associated String currency codes; 
     return currencyCodes.toArray(new String[currencyCodes.size()]); 
    } 
} 
1

Pe的r moneta的註釋,貨幣只能通過貨幣代碼或區域設置訪問。此實現不提供數字代碼訪問。這裏是你將如何通過貨幣代碼USD訪問:

CurrencyQuery query = CurrencyQueryBuilder.of().setCurrencyCodes("USD").build(); 
CurrencyUnit cu = Monetary.getCurrency(query); 
MonetaryAmount amount = FastMoney.of(99.99, cu); 
System.out.println(MonetaryFormats.getAmountFormat(Locale.ENGLISH).format(amount)); 

如果你想貢獻基於數字代碼查詢,我建議你接觸到項目本身。

或者,您可以根據數字代碼getCurrencies()和過濾檢索所有貨幣:

CurrencyUnit cu = Monetary.getCurrencies().stream() 
       .filter(c -> c.getNumericCode() == 840) 
       .findAny() 
       .orElseThrow(RuntimeException::new); 

最後,你可以implement your own CurrencyProvider實現getCurrency基於數字代碼進行查詢。

+0

謝謝,它的一個好點,但他們沒有明確表示不支持。我喜歡增加支持的想法,將會研究它。 –

+0

@ andrew-g-za添加了一些其他選項,允許您根據數字代碼訪問貨幣 –