How to get NumberFormat instance from currency code?

前端 未结 6 1351
無奈伤痛
無奈伤痛 2020-12-15 05:25

How can I get a NumberFormat (or DecimalFormat) instance corresponding to an ISO 4217 currency code (such as \"EUR\" or \"USD\") in order to format

6条回答
  •  不思量自难忘°
    2020-12-15 05:34

    public class PriceHelper {
        public static String formatPrice(Context context, String currencyCode, 
                                          double price) {
            if (price == 0) {
                return context.getString(R.string.free);
            }
            Currency currency = Currency.getInstance(currencyCode);
            NumberFormat format = NumberFormat.getCurrencyInstance();
            format.setCurrency(currency);
            return format.format(price);
        }
    }
    

提交回复
热议问题