Class to format and parse a text string such as 'EUR 25.25' or vice versa using BigDecimal default formatting.
This class will used as default by toString and parse in all implementation on Moneta.
By default this formatter formats the amounts as AMOUNT.DECIMAL CURRENCY, e.g. {code 1234 CHF}.
Hereby the currency is represented by its code and the amount formatted as BigDecimal, using its scale.
The decimal separator is always a dot.
MonetaryContext its maxScale and fixedScale attributes allow to configure the output,
by checking them against the scale of the BigDecimal.
Money money1 = Money.of(1234567.3456, "EUR");
System.out.println(money1.toString());
prints "EUR 1234567.3456", while:
Money money2 = Money.of(1234567.3456, "EUR", MonetaryContextBuilder.of().setMaxScale(2).build());
System.out.println(money2.toString());
prints "EUR 1234567.34". And:
Money money3 = Money.of(1234567.3, "EUR", MonetaryContextBuilder.of().setMaxScale(2).setFixedScale(true).build());
System.out.println(money2.toString());
prints "EUR 1234567.30".
org.javamoney.toStringFormatOrder configuration property:
'ca', 'c-a', 'c a'.'ac', 'a-c', 'a c' or
any other (default).Note
This formatter is active by default, but can be replaced with the standard JDK formatter by setting the
org.javamoney.moneta.useJDKdefaultFormat configuration property to true.
Money#toString()
Money#parse(CharSequence)
FastMoney#toString()
FastMoney#parse(CharSequence)
RoundedMoney#toString()
RoundedMoney#parse(CharSequence)
MonetaryContext
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| public static enum | ToStringMonetaryAmountFormat.
indicates with implementation will used to format or parser in ToStringMonetaryAmountFormat |
| Modifier and Type | Field and Description |
|---|---|
| private final AmountFormatContext | |
| private static final String | |
| private static final ToStringMonetaryAmountFormat | |
| private static final ToStringMonetaryAmountFormat | |
| private static final ToStringMonetaryAmountFormat | |
| private final ToStringMonetaryAmountFormat. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public AmountFormatContext | getContext()
Implements javax. The |
| public static ToStringMonetaryAmountFormat | |
| public MonetaryAmount | parse(CharSequence text)
Implements javax. Fully parses the text into an instance of |
| private ToStringMonetaryAmountFormat. | |
| public void | print(Appendable appendable, MonetaryAmount amount)
Implements javax. Formats the given |
| public String | queryFrom(MonetaryAmount amount)
Implements javax. Queries the specified monetary amount. |
| context | back to summary |
|---|---|
| private final AmountFormatContext context | |
| CONTEXT_PREFIX | back to summary |
|---|---|
| private static final String CONTEXT_PREFIX | |
| INSTANCE_FASTMONEY | back to summary |
|---|---|
| private static final ToStringMonetaryAmountFormat INSTANCE_FASTMONEY | |
| INSTANCE_MONEY | back to summary |
|---|---|
| private static final ToStringMonetaryAmountFormat INSTANCE_MONEY | |
| INSTANCE_ROUNDEDMONEY | back to summary |
|---|---|
| private static final ToStringMonetaryAmountFormat INSTANCE_ROUNDEDMONEY | |
| style | back to summary |
|---|---|
| private final ToStringMonetaryAmountFormat. | |
| ToStringMonetaryAmountFormat | back to summary |
|---|---|
| private ToStringMonetaryAmountFormat(ToStringMonetaryAmountFormat. | |
| getContext | back to summary |
|---|---|
| public AmountFormatContext getContext() Implements javax. Doc from javax. The
| |
| of | back to summary |
|---|---|
| public static ToStringMonetaryAmountFormat of(ToStringMonetaryAmountFormat. | |
| parse | back to summary |
|---|---|
| public MonetaryAmount parse(CharSequence text) throws MonetaryParseException Implements javax. Doc from javax. Fully parses the text into an instance of 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
| |
| parserMonetaryAmount | back to summary |
|---|---|
| private ToStringMonetaryAmountFormat. | |
| back to summary | |
|---|---|
| public void print(Appendable appendable, MonetaryAmount amount) throws IOException Implements javax. Doc from javax. Formats the given
Example implementations of
| |
| queryFrom | back to summary |
|---|---|
| public String queryFrom(MonetaryAmount amount) Implements javax. Doc from javax. Queries the specified monetary amount. This queries the specified monetary amount to return an object using the logic encapsulated in the implementing class. Examples might be a query that checks if the amount is positive, or one that extracts the currency as a symbol.
There are two equivalent ways of using a // these two lines are equivalent, but the second approach is recommended monetary = thisQuery.queryFrom(monetary); monetary = monetary.query(thisQuery);It is recommended to use the second approach, query(MonetaryQuery), as it is a lot clearer to read in code.
Implementation specificationThe implementation must take the input object and query it. The implementation defines the logic of the query and is responsible for documenting that logic. It may use any method onMonetaryAmount
to determine the result. The input object must not be altered.
This method may be called from multiple threads in parallel. It must be thread-safe when invoked.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final CurrencyUnit | |
| private final BigDecimal |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| currencyUnit | back to summary |
|---|---|
| private final CurrencyUnit currencyUnit | |
| number | back to summary |
|---|---|
| private final BigDecimal number | |
| ParserMonetaryAmount | back to summary |
|---|---|
| pack-priv ParserMonetaryAmount(CurrencyUnit currencyUnit, BigDecimal number) | |
| Modifier and Type | Field and Description |
|---|---|
| public static final ToStringMonetaryAmountFormat. | |
| public static final ToStringMonetaryAmountFormat. | |
| public static final ToStringMonetaryAmountFormat. | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv abstract MonetaryAmount | |
| public static ToStringMonetaryAmountFormat. | |
| public static ToStringMonetaryAmountFormat. |
| FAST_MONEY | back to summary |
|---|---|
| public static final ToStringMonetaryAmountFormat. | |
| MONEY | back to summary |
|---|---|
| public static final ToStringMonetaryAmountFormat. | |
| ROUNDED_MONEY | back to summary |
|---|---|
| public static final ToStringMonetaryAmountFormat. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| ToStringMonetaryAmountFormatStyle | back to summary |
|---|---|
| private ToStringMonetaryAmountFormatStyle() | |
| to | back to summary |
|---|---|
| pack-priv abstract MonetaryAmount to(ToStringMonetaryAmountFormat. | |
| valueOf | back to summary |
|---|---|
| public static ToStringMonetaryAmountFormat. | |
| values | back to summary |
|---|---|
| public static ToStringMonetaryAmountFormat. | |