Class MoneyAmountStyle

  • All Implemented Interfaces:
    Serializable

    public final class MoneyAmountStyle
    extends Object
    implements Serializable
    Defines the style that the amount of a monetary value will be formatted with.

    The style contains a number of fields that may be configured based on the locale:

    • character used for zero, which defined all the numbers from zero to nine
    • character used for positive and negative symbols
    • character used for the decimal point
    • whether and how to group the amount
    • character used for grouping, such as grouping thousands
    • size for each group, such as 3 for thousands
    • whether to always use a decimal point

    The style can be used in three basic ways.

    • set all the fields manually, resulting in the same amount style for all locales
    • call localize(java.util.Locale) manually and optionally adjust to set as required
    • leave the localized fields as null and let the locale in the formatter to determine the style

    This class is immutable and thread-safe.

    See Also:
    Serialized Form
    • Field Detail

      • ASCII_DECIMAL_POINT_GROUP3_COMMA

        public static final MoneyAmountStyle ASCII_DECIMAL_POINT_GROUP3_COMMA
        A style that uses ASCII digits/negative sign, the decimal point and groups large amounts in 3's using a comma. Forced decimal point is disabled.
      • ASCII_DECIMAL_POINT_GROUP3_SPACE

        public static final MoneyAmountStyle ASCII_DECIMAL_POINT_GROUP3_SPACE
        A style that uses ASCII digits/negative sign, the decimal point and groups large amounts in 3's using a space. Forced decimal point is disabled.
      • ASCII_DECIMAL_POINT_NO_GROUPING

        public static final MoneyAmountStyle ASCII_DECIMAL_POINT_NO_GROUPING
        A style that uses ASCII digits/negative sign, the decimal point and no grouping of large amounts. Forced decimal point is disabled.
      • ASCII_DECIMAL_COMMA_GROUP3_DOT

        public static final MoneyAmountStyle ASCII_DECIMAL_COMMA_GROUP3_DOT
        A style that uses ASCII digits/negative sign, the decimal comma and groups large amounts in 3's using a dot. Forced decimal point is disabled.
      • ASCII_DECIMAL_COMMA_GROUP3_SPACE

        public static final MoneyAmountStyle ASCII_DECIMAL_COMMA_GROUP3_SPACE
        A style that uses ASCII digits/negative sign, the decimal comma and groups large amounts in 3's using a space. Forced decimal point is disabled.
      • ASCII_DECIMAL_COMMA_NO_GROUPING

        public static final MoneyAmountStyle ASCII_DECIMAL_COMMA_NO_GROUPING
        A style that uses ASCII digits/negative sign, the decimal point and no grouping of large amounts. Forced decimal point is disabled.
      • LOCALIZED_GROUPING

        public static final MoneyAmountStyle LOCALIZED_GROUPING
        A style that will be filled in with localized values using the locale of the formatter. Grouping is enabled. Forced decimal point is disabled.
      • LOCALIZED_NO_GROUPING

        public static final MoneyAmountStyle LOCALIZED_NO_GROUPING
        A style that will be filled in with localized values using the locale of the formatter. Grouping is disabled. Forced decimal point is disabled.
    • Method Detail

      • of

        public static MoneyAmountStyle of​(Locale locale)
        Gets a localized style.

        This creates a localized style for the specified locale. Grouping will be enabled, forced decimal point will be disabled, absolute values will be disabled.

        Parameters:
        locale - the locale to use, not null
        Returns:
        the new instance, never null
      • localize

        public MoneyAmountStyle localize​(Locale locale)
        Returns a MoneyAmountStyle instance configured for the specified locale.

        This method will return a new instance where each field that was defined to be localized (by being set to null) is filled in. If this instance is fully defined (with all fields non-null), then this method has no effect. Once this method is called, no method will return null.

        The settings for the locale are pulled from DecimalFormatSymbols and DecimalFormat.

        Parameters:
        locale - the locale to use, not null
        Returns:
        the new instance for chaining, never null
      • getZeroCharacter

        public Character getZeroCharacter()
        Gets the character used for zero, and defining the characters zero to nine.

        The UTF-8 standard supports a number of different numeric scripts. Each script has the characters in order from zero to nine. This method returns the zero character, which therefore also defines one to nine.

        Returns:
        the zero character, null if to be determined by locale
      • withZeroCharacter

        public MoneyAmountStyle withZeroCharacter​(Character zeroCharacter)
        Returns a copy of this style with the specified zero character.

        The UTF-8 standard supports a number of different numeric scripts. Each script has the characters in order from zero to nine. This method sets the zero character, which therefore also defines one to nine.

        For English, this is a '0'. Some other scripts use different characters for the numbers zero to nine.

        Parameters:
        zeroCharacter - the zero character, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
      • getPositiveSignCharacter

        public Character getPositiveSignCharacter()
        Gets the character used for the positive sign character.

        The standard ASCII symbol is '+'.

        Returns:
        the format for positive amounts, null if to be determined by locale
      • withPositiveSignCharacter

        public MoneyAmountStyle withPositiveSignCharacter​(Character positiveCharacter)
        Returns a copy of this style with the specified positive sign character.

        The standard ASCII symbol is '+'.

        Parameters:
        positiveCharacter - the positive character, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
      • getNegativeSignCharacter

        public Character getNegativeSignCharacter()
        Gets the character used for the negative sign character.

        The standard ASCII symbol is '-'.

        Returns:
        the format for negative amounts, null if to be determined by locale
      • withNegativeSignCharacter

        public MoneyAmountStyle withNegativeSignCharacter​(Character negativeCharacter)
        Returns a copy of this style with the specified negative sign character.

        The standard ASCII symbol is '-'.

        Parameters:
        negativeCharacter - the negative character, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
      • getDecimalPointCharacter

        public Character getDecimalPointCharacter()
        Gets the character used for the decimal point.
        Returns:
        the decimal point character, null if to be determined by locale
      • withDecimalPointCharacter

        public MoneyAmountStyle withDecimalPointCharacter​(Character decimalPointCharacter)
        Returns a copy of this style with the specified decimal point character.

        For English, this is a dot.

        Parameters:
        decimalPointCharacter - the decimal point character, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
      • getGroupingCharacter

        public Character getGroupingCharacter()
        Gets the character used to separate groups, typically thousands.
        Returns:
        the grouping character, null if to be determined by locale
      • withGroupingCharacter

        public MoneyAmountStyle withGroupingCharacter​(Character groupingCharacter)
        Returns a copy of this style with the specified grouping character.

        For English, this is a comma.

        Parameters:
        groupingCharacter - the grouping character, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
      • getGroupingSize

        public Integer getGroupingSize()
        Gets the size of each group, typically 3 for thousands.
        Returns:
        the size of each group, null if to be determined by locale
      • withGroupingSize

        public MoneyAmountStyle withGroupingSize​(Integer groupingSize)
        Returns a copy of this style with the specified grouping size.
        Parameters:
        groupingSize - the size of each group, such as 3 for thousands, not zero or negative, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
        Throws:
        IllegalArgumentException - if the grouping size is zero or less
      • getExtendedGroupingSize

        public Integer getExtendedGroupingSize()
        Gets the size of each group, not typically used.

        This is primarily used to enable the Indian Number System, where the group closest to the decimal point is of size 3 and other groups are of size 2. The extended grouping size is used for groups that are not next to the decimal point. The value zero is used to indicate that extended grouping is not needed.

        Returns:
        the size of each group, null if to be determined by locale
      • withExtendedGroupingSize

        public MoneyAmountStyle withExtendedGroupingSize​(Integer extendedGroupingSize)
        Returns a copy of this style with the specified extended grouping size.
        Parameters:
        extendedGroupingSize - the size of each group, such as 3 for thousands, not zero or negative, null if to be determined by locale
        Returns:
        the new instance for chaining, never null
        Throws:
        IllegalArgumentException - if the grouping size is zero or less
      • getGroupingStyle

        public GroupingStyle getGroupingStyle()
        Gets the style of grouping required.
        Returns:
        the grouping style, not null
      • withGroupingStyle

        public MoneyAmountStyle withGroupingStyle​(GroupingStyle groupingStyle)
        Returns a copy of this style with the specified grouping setting.
        Parameters:
        groupingStyle - the grouping style, not null
        Returns:
        the new instance for chaining, never null
      • isForcedDecimalPoint

        public boolean isForcedDecimalPoint()
        Gets whether to always use the decimal point, even if there is no fraction.
        Returns:
        whether to force the decimal point on output
      • withForcedDecimalPoint

        public MoneyAmountStyle withForcedDecimalPoint​(boolean forceDecimalPoint)
        Returns a copy of this style with the specified decimal point setting.
        Parameters:
        forceDecimalPoint - true to force the use of the decimal point, false to use it if required
        Returns:
        the new instance for chaining, never null
      • isAbsValue

        public boolean isAbsValue()
        Returns true if the absolute value setting.

        If this is set to true, the absolute (unsigned) value will be output. If this is set to false, the signed value will be output. Note that when parsing, signs are accepted.

        Returns:
        true to output the absolute value, false for the signed value
      • withAbsValue

        public MoneyAmountStyle withAbsValue​(boolean absValue)
        Returns a copy of this style with the specified absolute value setting.

        If this is set to true, the absolute (unsigned) value will be output. If this is set to false, the signed value will be output. Note that when parsing, signs are accepted.

        Parameters:
        absValue - true to output the absolute value, false for the signed value
        Returns:
        the new instance for chaining, never null
      • equals

        public boolean equals​(Object other)
        Compares this style with another.
        Overrides:
        equals in class Object
        Parameters:
        other - the other style, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        A suitable hash code.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • toString

        public String toString()
        Gets a string summary of the style.
        Overrides:
        toString in class Object
        Returns:
        a string summarising the style, never null