@Test public void formatPriceString_whenUSLocale_rendersCorrectSymbols() { Locale.setDefault(Locale.US); assertEquals(PaymentUtils.formatPriceString(12300, Currency.getInstance("USD")), "$123.00"); Currency euro = Currency.getInstance(Locale.GERMANY); assertEquals(PaymentUtils.formatPriceString(12300, euro), "EUR123.00"); Currency canadianDollar = Currency.getInstance(Locale.CANADA); assertEquals(PaymentUtils.formatPriceString(12300, canadianDollar), "CAD123.00"); Currency britishPound = Currency.getInstance(Locale.UK); assertEquals(PaymentUtils.formatPriceString(10000, britishPound), "GBP100.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); }    }