Top Description Inners Fields Constructors Methods
org.javamoney.moneta.format

public final Class ToStringMonetaryAmountFormat

extends Object
implements MonetaryAmountFormat
Class Inheritance
All Implemented Interfaces
javax.money.format.MonetaryAmountFormat, javax.money.MonetaryQuery
Imports
org.javamoney.moneta.FastMoney, .Money, .RoundedMoney, org.javamoney.moneta.spi.MonetaryConfig, java.io.IOException, java.math.BigDecimal, .RoundingMode, java.util.Objects, .Optional, .Set, javax.money.CurrencyUnit, .MonetaryAmount, .Monetary, .MonetaryContext, javax.money.format.AmountFormatContext, .AmountFormatContextBuilder, .MonetaryAmountFormat, .MonetaryParseException

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.


If the amount contains a MonetaryContext its maxScale and fixedScale attributes allow to configure the output, by checking them against the scale of the BigDecimal.
For example:

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".

You can configure the order of formatting with the org.javamoney.toStringFormatOrder configuration property: Parsing should work either with the currency prefixed or postfixed

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)

Authors
Otavio Santana, Anatole Tresch, Werner Keil
Version
1.8
See Also
MonetaryContext

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static enum
ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle

indicates with implementation will used to format or parser in ToStringMonetaryAmountFormat

Field Summary

Modifier and TypeField and Description
private final AmountFormatContext
private static final String
private static final ToStringMonetaryAmountFormat
private static final ToStringMonetaryAmountFormat
private static final ToStringMonetaryAmountFormat
private final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public AmountFormatContext
public static ToStringMonetaryAmountFormat
public MonetaryAmount
parse(CharSequence text)

Implements javax.money.format.MonetaryAmountFormat.parse.

Fully parses the text into an instance of MonetaryAmount.

private ToStringMonetaryAmountFormat.ParserMonetaryAmount
public String
queryFrom(MonetaryAmount amount)

Implements javax.money.MonetaryQuery.queryFrom.

Queries the specified monetary amount.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
private final AmountFormatContext context
CONTEXT_PREFIXback to summary
private static final String CONTEXT_PREFIX
INSTANCE_FASTMONEYback to summary
private static final ToStringMonetaryAmountFormat INSTANCE_FASTMONEY
INSTANCE_MONEYback to summary
private static final ToStringMonetaryAmountFormat INSTANCE_MONEY
INSTANCE_ROUNDEDMONEYback to summary
private static final ToStringMonetaryAmountFormat INSTANCE_ROUNDEDMONEY
styleback to summary
private final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle style

Constructor Detail

ToStringMonetaryAmountFormatback to summary
private ToStringMonetaryAmountFormat(ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle style)

Method Detail

getContextback to summary
public AmountFormatContext getContext()

Implements javax.money.format.MonetaryAmountFormat.getContext.

Doc from javax.money.format.MonetaryAmountFormat.getContext.

The AmountFormatContext to be applied when a MonetaryAmount is formatted.

Annotations
@Override
ofback to summary
public static ToStringMonetaryAmountFormat of(ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle style)
parseback to summary
public MonetaryAmount parse(CharSequence text) throws MonetaryParseException

Implements javax.money.format.MonetaryAmountFormat.parse.

Doc from javax.money.format.MonetaryAmountFormat.parse.

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.

Annotations
@Override
Exceptions
MonetaryParseException:
if there is a problem while parsing
parserMonetaryAmountback to summary
private ToStringMonetaryAmountFormat.ParserMonetaryAmount parserMonetaryAmount(CharSequence text)
queryFromback to summary
public String queryFrom(MonetaryAmount amount)

Implements javax.money.MonetaryQuery.queryFrom.

Doc from javax.money.MonetaryQuery.queryFrom.

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 MonetaryQuery. The first is to invoke the method on this interface. The second is to use MonetaryAmount#query(MonetaryQuery):

// 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 specification

The 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 on MonetaryAmount 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.

Annotations
@Override
org.javamoney.moneta.format back to summary

private Class ToStringMonetaryAmountFormat.ParserMonetaryAmount

extends Object
Class Inheritance
  • java.lang.Object
  • org.javamoney.moneta.format.ToStringMonetaryAmountFormat.ParserMonetaryAmount

Field Summary

Modifier and TypeField and Description
private final CurrencyUnit
private final BigDecimal

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currencyUnitback to summary
private final CurrencyUnit currencyUnit
numberback to summary
private final BigDecimal number

Constructor Detail

ParserMonetaryAmountback to summary
pack-priv ParserMonetaryAmount(CurrencyUnit currencyUnit, BigDecimal number)
org.javamoney.moneta.format back to summary

public sealed Enum ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle

extends Enum<ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle>
Class Inheritance

indicates with implementation will used to format or parser in ToStringMonetaryAmountFormat

Field Summary

Modifier and TypeField and Description
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle
private static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract MonetaryAmount
public static ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle
public static ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

FAST_MONEYback to summary
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle FAST_MONEY

FastMoney

MONEYback to summary
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle MONEY

Money

ROUNDED_MONEYback to summary
public static final ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle ROUNDED_MONEY

RoundedMoney

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ToStringMonetaryAmountFormatStyleback to summary
private ToStringMonetaryAmountFormatStyle()

Method Detail

toback to summary
pack-priv abstract MonetaryAmount to(ToStringMonetaryAmountFormat.ParserMonetaryAmount amount)
valueOfback to summary
public static ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle valueOf(String name)
valuesback to summary
public static ToStringMonetaryAmountFormat.ToStringMonetaryAmountFormatStyle[] values()