Class NumberFormat

java.lang.Object
com.aquima.interactions.foundation.text.NumberFormat
All Implemented Interfaces:
INumberFormat, Serializable

public class NumberFormat extends Object implements INumberFormat
NumberFormat is an implementation for INumberFormat which formats decimals to string and can parse strings representations back into numbers. A NumberFormat uses a similar syntax like the java DecimalFormat class but with some exceptions. The syntax is geared at number display only. Currency etc is not part of this specification. A NumberFormat pattern can be divided into 4 parts.

Syntax: {options}format;negative-spec;positive-spec

  • options

    The option set is optional and must be surrounded with {} when used. It represents a key value mechanism separated by ',' signs. Examples include: a=b,q=e meaning a is set to b and q is set to e. Valid options are:

    • locale=nl-NL

      This specifies a locale in the option set overriding the one of the NumberFormats' class.

  • format

    The format pattern which actually specified how the number should look like consists of a decimal part and an * fractional part. divided by a '.' when a fractional part is required.

    • decimal

      Decimal part specifies how grouping, minimum decimal and maximum decimal digits. For example a pattern: #,##0 specifies at least 1 decimal digit ( which is always the case ) and grouping every 3 decimal digits. With a maximumof 4 decimal digits. At least 1 zero at the end of the decimal syntax is required for the pattern to parse.

    • fractional

      The fractional part begins if there is a decimal separator '.'. If there is no decimal separator there is no fractional part. The fractional part specifies how many minimum fraction digits there are required and how many fraction digits at max are allowed. For example a syntax 0.00## will specify that at least two fractional should be displayed and at most four.

  • negative-spec

    Negative pattern has only 2 special characters: '#' and ';'. The '#' will dictate the actual number pattern specified earlier. For example: 0.0;(#) will specifiy that a negative number is prefixed with '(' and postfixed with ')' While * ';' will end the pattern part and proceed to the positive specification

  • positive-spec

    The positive pattern has only 1 special character: '#'. The '#' will dictate the actual number pattern specified earlier. For example: 0.0;(#) will specifiy that a negative number is prefixed with '(' and postfixed with ')'

Examples:
  • "##0" output is like 23 for US locale's and will not use or accept any fractional digits string
  • "##0.00" output is like 23.00 for US locale's
  • "000.00" output is like 023.00 for US locale's
  • "000.00;-#" -23 outputs like -023.00 for US locale's
  • "000.00;-#;+#" 23 outputs like +023.00 for US locale's
  • "{locale=NL-nl}000.00;-#;+#" 23 outputs like +023,00 for the specified NL locale.
Since:
6.0
Author:
F. van der Meer
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    NumberFormat(Locale locale, String pattern)
    Constructs a number format based on a locale and a pattern.
  • Method Summary

    Modifier and Type
    Method
    Description
    format(double number)
    Formats an integer number to it's string representation.
    format(long number)
    Formats an integer number to it's string representation.
    double
    parse(String text)
    This method parses an input text and returns the corresponding number value.
    long
    This method parses an input text and returns the corresponding number value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NumberFormat

      public NumberFormat(Locale locale, String pattern)
      Constructs a number format based on a locale and a pattern. The pattern is parsed according to the specifications of NumberFormat
      Parameters:
      locale - The locale from which the system gains the decimal symbols.
      pattern - The pattern specifying the decimal pattern as described in the class header.
      Throws:
      PatternException - When the pattern was invalid.
  • Method Details

    • format

      public String format(long number)
      Description copied from interface: INumberFormat
      Formats an integer number to it's string representation. The implementor of this interface should be able to parse string that is returned as well.
      Specified by:
      format in interface INumberFormat
      Parameters:
      number - The number to format.
      Returns:
      The string representation of this number which should never be null
    • format

      public String format(double number)
      Description copied from interface: INumberFormat
      Formats an integer number to it's string representation. The implementor of this interface should be able to parse the string that was returned as well.
      Specified by:
      format in interface INumberFormat
      Parameters:
      number - The number to format.
      Returns:
      The string representation of this number which should never be null
    • parse

      public double parse(String text)
      Description copied from interface: INumberFormat
      This method parses an input text and returns the corresponding number value. The complete text should be a number during parse. If the parse fails an
      Specified by:
      parse in interface INumberFormat
      Parameters:
      text - The text to parse, which should never be a null pointer.
      Returns:
      The double value parsed from the text.
    • parseInteger

      public long parseInteger(String text)
      Description copied from interface: INumberFormat
      This method parses an input text and returns the corresponding number value. The complete text should be a number during parse. If the parse fails an
      Specified by:
      parseInteger in interface INumberFormat
      Parameters:
      text - The text to parse, which should never be a null pointer.
      Returns:
      The double value parsed from the text.