@Test public void formatPriceString_whenInternationalLocale_rendersCorrectSymbols() { Locale.setDefault(Locale.GERMANY); Currency euro = Currency.getInstance(Locale.GERMANY); assertEquals(PaymentUtils.formatPriceString(10000, euro), "100,00 €"); Locale.setDefault(Locale.JAPAN); Currency yen = Currency.getInstance(Locale.JAPAN); assertEquals(PaymentUtils.formatPriceString(100, yen), "￥100"); Locale.setDefault(new Locale("ar", "JO")); Currency jordanianDinar = Currency.getInstance("JOD"); assertEquals(PaymentUtils.formatPriceString(100123, jordanianDinar), jordanianDinar.getSymbol() + " 100.123"); Locale.setDefault(Locale.UK); Currency britishPound = Currency.getInstance(Locale.UK); assertEquals(PaymentUtils.formatPriceString(10000, britishPound), "£100.00"); Locale.setDefault(Locale.CANADA); Currency canadianDollar = Currency.getInstance(Locale.CANADA); assertEquals(PaymentUtils.formatPriceString(12300, canadianDollar), "$123.00"); }
static String formatPriceString(double amount, @NonNull Currency currency) { double majorUnitAmount = amount / Math.pow(10, currency.getDefaultFractionDigits()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(currency.getSymbol(Locale.getDefault())); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); return currencyFormat.format(majorUnitAmount); }
PaymentUtils { static String formatPriceString(double amount, @NonNull Currency currency) { double majorUnitAmount = amount / Math.pow(10, currency.getDefaultFractionDigits()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(currency.getSymbol(Locale.getDefault())); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); return currencyFormat.format(majorUnitAmount); } }
PaymentUtils { static String formatPriceString(double amount, @NonNull Currency currency) { double majorUnitAmount = amount / Math.pow(10, currency.getDefaultFractionDigits()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(currency.getSymbol(Locale.getDefault())); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); return currencyFormat.format(majorUnitAmount); }  }
PaymentUtils { static String formatPriceString(double amount, @NonNull Currency currency) { double majorUnitAmount = amount / Math.pow(10, currency.getDefaultFractionDigits()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(currency.getSymbol(Locale.getDefault())); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); return currencyFormat.format(majorUnitAmount); }   }
PaymentUtils { static String formatPriceString(double amount, @NonNull Currency currency) { double majorUnitAmount = amount / Math.pow(10, currency.getDefaultFractionDigits()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) currencyFormat).getDecimalFormatSymbols(); decimalFormatSymbols.setCurrencySymbol(currency.getSymbol(Locale.getDefault())); ((java.text.DecimalFormat) currencyFormat).setDecimalFormatSymbols(decimalFormatSymbols); return currencyFormat.format(majorUnitAmount); }    }