Top Description Methods
javax.money.format

public Interface MonetaryAmountFormat

extends MonetaryQuery<String>
Imports
javax.money.MonetaryAmount, .MonetaryContext, .MonetaryQuery, java.io.IOException

Formats instances of MonetaryAmount to a String or an Appendable.

To obtain a MonetaryAmountFormat for a specific locale, including the default locale, call MonetaryFormats#getAmountFormat(java.util.Locale, String...). More complex formatting scenarios can be implemented by registering instances of .MonetaryAmountFormatProviderSpi. The spi implementation creates new instances of javax.money.format.MonetaryAmountFormat based on the styleId and (arbitrary) attributes passed within the javax.money.format.AmountFormatContext.

In general, do prefer accessing MonetaryAmountFormat instances from the MonetaryFormats singleton, instead of instantiating implementations directly, since the MonetaryFormats factory method may return different subclasses or may implement contextual behaviour (in a EE context). If you need to customize the format object, do something like this:


MonetaryAmountFormat f = MonetaryFormats.getInstance(loc);
f.setStyle(f.getStyle().toBuilder().setPattern("###.##;(###.##)").build());

Special Values

Negative zero ("-0") should always parse to

Synchronization

Instances of this class are not required to be thread-safe. It is recommended to of separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

Example


// Print out a number using the localized number, currency,
// for each locale
Locale[] locales = MonetaryFormats.getAvailableLocales();
MonetaryAmount amount = ...;
MonetaryAmountFormat form;
    System.out.println("FORMAT");
    for (int i = 0; i < locales.length; ++i) {
        if (locales[i].getCountry().length() == 0) {
           continue; // Skip language-only locales
        }
        System.out.print(locales[i].getDisplayName());
        form = MonetaryFormats.getInstance(locales[i]);
        System.out.print(": " + form.getStyle().getPattern());
        String myAmount = form.format(amount);
        System.out.print(" -> " + myAmount);
        try {
            System.out.println(" -> " + form.parse(form.format(myAmount)));
        } catch (ParseException e) {}
    }
}

Method Summary

Modifier and TypeMethod and Description
public default String

Returns:

the string printed using the settings of this formatter
format
(MonetaryAmount
the amount to format, not null
amount
)

Formats the given MonetaryAmount to a String.

public AmountFormatContext

Returns:

the AmountFormatContext used, never null.
getContext
()

The AmountFormatContext to be applied when a MonetaryAmount is formatted.

public MonetaryAmount

Returns:

the parsed value, never null
parse
(CharSequence
the text to parse, not null
text
)

Fully parses the text into an instance of MonetaryAmount.

Inherited from javax.money.MonetaryQuery:
queryFrom

Method Detail

formatback to summary
public default String format(MonetaryAmount amount)

Formats the given MonetaryAmount to a String.

Parameters
amount:MonetaryAmount

the amount to format, not null

Returns:String

the string printed using the settings of this formatter

Exceptions
UnsupportedOperationException:
if the formatter is unable to print
IllegalStateException:
if an IO error occurs.
getContextback to summary
public AmountFormatContext getContext()

The AmountFormatContext to be applied when a MonetaryAmount is formatted.

Returns:AmountFormatContext

the AmountFormatContext used, never null.

parseback to summary
public MonetaryAmount parse(CharSequence text) throws MonetaryParseException

Fully parses the text into an instance of MonetaryAmount.

The parse must complete normally and parse the entire text. If the parse completes without reading the entire length of the text, an exception is thrown. If any other problem occurs during parsing, an exception is thrown.

Additionally the effective implementation type returned can be determined by the MonetaryContext applied to the MonetaryAmountFormat. This formatter will call javax.money.Monetary#getDefaultAmountType() and will use the result returned to access a corresponding javax.money.MonetaryAmountFactory to of the instance returned.

Parameters
text:CharSequence

the text to parse, not null

Returns:MonetaryAmount

the parsed value, never null

Exceptions
MonetaryParseException:
if there is a problem while parsing
UnsupportedOperationException:
if the formatter is unable to parse