Formatting is handled by MonetaryAmountFormat instances, which are thread-safe and immutable. They can be accessed via the MonetaryFormats singleton.
Formats can be retrieved by:
- Locale: e.g.,
MonetaryFormats.getAmountFormat(Locale.GERMANY). - Name: e.g.,
MonetaryFormats.getAmountFormat("MyCustomFormat"). - Query: Using
AmountFormatQueryBuilder to specify complex parameters like strict, omitNegative, or custom negative sign strings.
Once you have a format, use .format(amount) to get a string, or use the format to parse a string back into a MonetaryAmount.
// Accessing formats
MonetaryAmountFormat formatCountry = MonetaryFormats.getAmountFormat(Locale.GERMANY);
MonetaryAmountFormat formatQueried = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of("MyCustomFormat2")
.set("strict", true)
.set("omitNegative", true)
.set("omitNegativeSign", "N/A")
.build()
);
// Formatting
String formattedString = format.format(amount);