@Override public String print(MonetaryAmount object, Locale locale) { return getMonetaryAmountFormat(locale).format(object); }
@Override public MonetaryAmount parse(String text, Locale locale) { return getMonetaryAmountFormat(locale).parse(text); }
String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." + "getMonetaryFormat(Locale,String...) with supported Locale: " + locale); assertFalse(formatProduced.isEmpty(), "MonetaryAmountFormat returned empty String for " + amt); try { MonetaryAmount amtParsed = format.parse(formatProduced); assertNotNull(amtParsed, "Reverse-parsing of MonetaryAmount failed for '" + formatProduced + "' using MonetaryAmountFormat: " + format);
/** * Formats the given {@link MonetaryAmount} to a String. * * @param amount the amount to format, not {@code null} * @return the string printed using the settings of this formatter * @throws UnsupportedOperationException if the formatter is unable to print * @throws IllegalStateException if an IO error occurs. */ default String format(MonetaryAmount amount){ StringBuilder b = new StringBuilder(); try{ print(b, amount); } catch(IOException e){ throw new IllegalStateException("Formatting error.", e); } return b.toString(); }
/** * AccessingMonetaryAmountFormat using * MonetaryFormats.getAmountFormat(Locale locale), all locales * available also from java.text.DecimalFormat must be supported. */ @SpecAssertion(section = "4.4.3", id = "443-A1") @Test(description = "4.4.3 Ensures all Locales defined by DecimalFormat.getAvailableLocales() are available for " + "monetary formatting.") public void testLocalesSupported() { Locale[] jdkDecimalFormatLocales = DecimalFormat.getAvailableLocales(); for (Locale jdkDecimalFormatLocale : jdkDecimalFormatLocales) { MonetaryAmountFormat amountFormat = MonetaryFormats.getAmountFormat(jdkDecimalFormatLocale); AssertJUnit.assertNotNull(amountFormat); assertEquals(jdkDecimalFormatLocale, amountFormat.getContext().getLocale()); } }
String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." + "getMonetaryFormat(Locale,String...) with supported Locale: " + locale); assertFalse(formatProduced.isEmpty(), "MonetaryAmountFormat returned empty String for " + amt); try { MonetaryAmount amtParsed = format.parse(formatProduced); assertNotNull(amtParsed, "Reverse-parsing of MonetaryAmount failed for '" + formatProduced + "' using MonetaryAmountFormat: " + format);
@Override public String print(MonetaryAmount object, Locale locale) { return getMonetaryAmountFormat(locale).format(object); }
String formatProduced = format.format(amt); assertNotNull(formatProduced, "No MonetaryAmountFormat returned from MonetaryFormats." + "getMonetaryFormat(Locale,String...) with supported Locale: " + locale); assertFalse(formatProduced.isEmpty(), "MonetaryAmountFormat returned empty String for " + amt); try { MonetaryAmount amtParsed = format.parse(formatProduced); assertNotNull(amtParsed, "Reverse-parsing of MonetaryAmount failed for '" + formatProduced + "' using MonetaryAmountFormat: " + format);
@Override public MonetaryAmount parse(String text, Locale locale) { return getMonetaryAmountFormat(locale).parse(text); }
@Override public void serialize(MonetaryAmount value, JsonGenerator jgen, SerializerProvider provider) throws IOException { if (value != null) { jgen.writeString(MonetaryFormats.getAmountFormat(LocaleContextHolder.getLocale()).format(value)); } else { jgen.writeNull(); } }
MonetaryAmountFactory<?> amountFactory = Monetary.getDefaultAmountFactory(); MonetaryAmount monetaryAmount = amountFactory.setCurrency(Monetary.getCurrency("EUR")).setNumber(12345.67).create(); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.getDefault()); System.out.println(format.format(monetaryAmount));
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.US); MonetaryAmount amount = format.parse("USD 1.23");
MonetaryAmountFactory<?> amountFactory = Monetary.getDefaultAmountFactory(); MonetaryAmount monetaryAmount = amountFactory.setCurrency(Monetary.getCurrency("EUR")).setNumber(12345.67).create(); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.getDefault()); System.out.println(format.format(monetaryAmount));
/** * Obtains an instance of FastMoney from a text using specific formatter. * * @param text the text to parse not null * @param formatter the formatter to use not null * @return RoundedMoney instance */ public static RoundedMoney parse(CharSequence text, MonetaryAmountFormat formatter) { return from(formatter.parse(text)); }
MonetaryAmountFactory<?> amountFactory = Monetary.getDefaultAmountFactory(); MonetaryAmount monetaryAmount = amountFactory.setCurrency(Monetary.getCurrency("EUR")).setNumber(12345.67).create(); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.getDefault()); System.out.println(format.format(monetaryAmount));
/** * Obtains an instance of Money from a text using specific formatter. * * @param text the text to parse not null * @param formatter the formatter to use not null * @return Money instance */ public static Money parse(CharSequence text, MonetaryAmountFormat formatter) { return from(formatter.parse(text)); }
MonetaryAmount monetaryAmount = Money.of(12345.67, "EUR"); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.getDefault()); System.out.println(format.format(monetaryAmount));
/** * Obtains an instance of FastMoney from a text using specific formatter. * * @param text the text to parse not null * @param formatter the formatter to use not null * @return FastMoney instance */ public static FastMoney parse(CharSequence text, MonetaryAmountFormat formatter) { return from(formatter.parse(text)); }
MonetaryAmount monetaryAmount = Money.of(12345.67, "EUR"); MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.getDefault()); System.out.println(format.format(monetaryAmount));