Java: Currency to Locale Mapping Possible?

后端 未结 4 1199
Happy的楠姐
Happy的楠姐 2020-12-16 12:11

I have a value stored in a DB correlating to a monetary amount, say 10.0. I also have access to the Currency/CurrencyCode. How can I use NumberFormat/DecimalFormat/(other?)

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 13:00

    JasonTrue is correct, but you can override the currency of the NumberFormat's locale:

    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
    //the user may have selected a different currency than the default for their locale
    Currency currency = Currency.getInstance("GBP");
    numberFormat.setCurrency(currency);
    numberFormat.format(amount);
    

提交回复
热议问题