/** * Creates a simple format query based on a singlet Locale, similar to {@link java.text.DecimalFormat#getInstance * (java.util.Locale)}. * * @param locale the target locale, not null. * @param providers the providers to be used, not null. * @return a new query instance */ public static AmountFormatQuery of(Locale locale, String... providers) { return AmountFormatQueryBuilder.of(locale).setProviderNames(providers).build(); }
/** * Access the default {@link MonetaryAmountFormat} given a {@link Locale}. * * @param formatName the target format name, not {@code null}. * @param providers The (optional) providers to be used, ordered correspondingly. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ default MonetaryAmountFormat getAmountFormat(String formatName, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(formatName).setProviderNames(providers).build()); }
/** * Access the default {@link MonetaryAmountFormat} given a {@link Locale}. * * @param locale the target {@link Locale}, not {@code null}. * @param providers The (optional) providers to be used, ordered correspondingly. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ default MonetaryAmountFormat getAmountFormat(Locale locale, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(locale).setProviderNames(providers).build()); }
@Override public String format(@Nullable final MonetaryAmount price) { return Optional.ofNullable(price).map(p -> { final boolean isDecimal = p.getNumber().doubleValueExact() % 1 != 0;//TODO this can be improved with monetary query final AmountFormatQuery pattern = AmountFormatQueryBuilder.of(locale) .set(CurrencyStyle.SYMBOL) .set(PATTERN, isDecimal ? PATTERN_WITH_DECIMAL : PATTERN_WITHOUT_DECIMAL) .build(); return MonetaryFormats.getAmountFormat(pattern).format(p); }).orElse(""); } }
/** * Access the a {@link MonetaryAmountFormat} given its styleId. * * @param formatName the target format name, not {@code null}. * @param providers The providers to be used, if not set the providers as defined by #getDefaultCurrencyProviderChain() are * used. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ public static MonetaryAmountFormat getAmountFormat(String formatName, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(formatName).setProviderNames(providers).build()); }
/** * Access the default {@link MonetaryAmountFormat} given a {@link Locale}. * * @param locale the target {@link Locale}, not {@code null}. * @param providers The providers to be queried, if not set the providers as defined by #getDefaultCurrencyProviderChain() * are queried. * @return the matching {@link MonetaryAmountFormat} * @throws MonetaryException if no registered {@link MonetaryAmountFormatProviderSpi} can provide a * corresponding {@link MonetaryAmountFormat} instance. */ public static MonetaryAmountFormat getAmountFormat(Locale locale, String... providers) { return getAmountFormat(AmountFormatQueryBuilder.of(locale).setProviderNames(providers).setLocale(locale).build()); }
public static void main(String... args) { MonetaryAmount amt = Money.of(1234.5678, "EUR"); System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY))); System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt)); amt = Money.of(123412341234.5678, "INR"); System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt)); // no with adaptive groupings System.out.println(MonetaryFormats.getAmountFormat( AmountFormatQueryBuilder.of(new Locale("", "INR")) .set(AmountFormatParams.GROUPING_SIZES, new int[]{2, 3}) .set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'}) .build()) .format(amt)); }
public static void main(String[] args) { CurrencyUnit dollar = Monetary.getCurrency(Locale.US); MonetaryAmount monetaryAmount = Money.of(1202.12D, dollar); MonetaryAmountFormat germanFormat = MonetaryFormats.getAmountFormat( Locale.GERMANY); MonetaryAmountFormat usFormat = MonetaryFormats.getAmountFormat( Locale.US); MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat( AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).build()); System.out.println(germanFormat.format(monetaryAmount));//1.202,12 USD System.out.println(usFormat.format(monetaryAmount));//USD1,202.12 System.out.println(customFormat.format(monetaryAmount));//$1,202.12 } }
AmountFormatQuery query = AmountFormatQueryBuilder.of(locale).setMonetaryAmountFactory(fact).build(); if (fact.getAmountType().equals(TestAmount.class)) { continue;
public static void main(String... args) { MonetaryAmount amt = Money.of(1234.5678, "EUR"); System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY))); System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt)); amt = Money.of(123412341234.5678, "INR"); System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt)); // no with adaptive groupings System.out.println(MonetaryFormats.getAmountFormat( AmountFormatQueryBuilder.of(new Locale("", "INR")) .set(AmountFormatParams.GROUPING_SIZES, new int[]{2, 3}) .set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'}) .build()) .format(amt)); amt = Money.of(5, "USD"); System.out.println(MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).set(AmountFormatParams.PATTERN, "¤##.##").build()).format(amt)); }