Class NumberFormatter


  • public final class NumberFormatter
    extends Object
    Provides the ability to parse and format numbers.

    This exists as an alternative to NumberFormat and DecimalFormat which are not thread-safe.

    Instances of this class are immutable and thread-safe.

    • Method Detail

      • of

        public static NumberFormatter of​(boolean grouped,
                                         int decimalPlaces)
        Obtains a standard formatter configured by grouping and decimal places.

        The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). Numbers will be rounded using RoundingMode.HALF_EVEN

        Parameters:
        grouped - true to group, false to not group
        decimalPlaces - the minimum number of decimal places, from 0 to 9
        Returns:
        the formatter
        Throws:
        IllegalArgumentException - if the decimal places is invalid
      • of

        public static NumberFormatter of​(boolean grouped,
                                         int minDecimalPlaces,
                                         int maxDecimalPlaces)
        Obtains a standard formatter configured by grouping and decimal places.

        The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). Numbers will be rounded using RoundingMode.HALF_EVEN

        Parameters:
        grouped - true to group, false to not group
        minDecimalPlaces - the minimum number of decimal places, from 0 to 9
        maxDecimalPlaces - the minimum number of decimal places, from 0 to 9
        Returns:
        the formatter
        Throws:
        IllegalArgumentException - if the decimal places is invalid
      • ofPercentage

        public static NumberFormatter ofPercentage​(boolean grouped,
                                                   int minDecimalPlaces,
                                                   int maxDecimalPlaces)
        Obtains a formatter for decimal percentages configured by grouping and decimal places.

        The formatter will have the specified number of decimal places. The integer part will be grouped if the flag is set. The decimal part will never be grouped or truncated. The implementation uses English locale data, which uses commas as a separator and a decimal point (dot). The formatter will suffix the output with '%'. Numbers will be rounded using RoundingMode.HALF_EVEN

        The number passed in must be the decimal representation of the percentage. It will be multiplied by 100 before formatting.

        Parameters:
        grouped - true to group, false to not group
        minDecimalPlaces - the minimum number of decimal places, from 0 to 9
        maxDecimalPlaces - the minimum number of decimal places, from 0 to 9
        Returns:
        the formatter
        Throws:
        IllegalArgumentException - if the decimal places is invalid
      • ofPattern

        public static NumberFormatter ofPattern​(String pattern,
                                                Locale locale)
        Obtains a formatter based on a pattern in the specified locale.
        Parameters:
        pattern - the pattern string to use
        locale - the locale to use
        Returns:
        the formatter
        Throws:
        IllegalArgumentException - if the pattern is invalid
        See Also:
        DecimalFormat
      • format

        public String format​(double number)
        Formats a double using this formatter.
        Parameters:
        number - the number to format
        Returns:
        the formatted string
      • format

        public String format​(long number)
        Formats a long using this formatter.
        Parameters:
        number - the number to format
        Returns:
        the formatted string
      • parse

        public double parse​(String text)
        Parses the specific string, returning a double.
        Parameters:
        text - the string to parse
        Returns:
        the parsed number
        Throws:
        IllegalArgumentException - if the text cannot be parsed
      • toString

        public String toString()
        Returns a string representation of this formatter.
        Overrides:
        toString in class Object
        Returns:
        the string