Skip to content

Instantly share code, notes, and snippets.

@gastonfournier
Last active May 13, 2022 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gastonfournier/8a34876c69009d9caf9329c7ddc03cc9 to your computer and use it in GitHub Desktop.
Save gastonfournier/8a34876c69009d9caf9329c7ddc03cc9 to your computer and use it in GitHub Desktop.
import javax.money.MonetaryAmount;
import javax.money.format.AmountFormatQueryBuilder;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import org.javamoney.moneta.Money;
import org.javamoney.moneta.format.CurrencyStyle;
import org.junit.jupiter.api.Test;
import java.util.Locale;
public class MonetaEURFormatTest {
private static final Locale EUR_PREFERRED_LOCALE = Locale.forLanguageTag("es-ES");
static String format(MonetaryAmount money, Locale locale) {
MonetaryAmountFormat formatter = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(locale)
.set(CurrencyStyle.SYMBOL)
.setLocale(EUR_PREFERRED_LOCALE)
.build()
);
return formatter.format(money);
}
@Test
void testFormat() {
MonetaryAmount monetaryAmount = Money.of(1234, "EUR");
String esFormat = format(monetaryAmount, Locale.forLanguageTag("es-ES"));
String usFormat = format(monetaryAmount, Locale.forLanguageTag("en-US"));
System.out.println(esFormat); // outputs 1.234,00 €
System.out.println(usFormat); // outputs 1.234,00 €
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment