Class Numbers


  • public final class Numbers
    extends Object

    Collection of EL functions for number formatting: of:formatBytes(), of:formatCurrency(), of:formatNumber(), of:formatNumberDefault(), of:formatPercent(), of:formatThousands() and of:formatThousandsUnit().

    Since:
    1.2
    Author:
    Bauke Scholtz
    • Method Detail

      • formatBytes

        public static String formatBytes​(Long bytes)
        Format the given bytes to nearest 10n with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction. For example:
        • 1023 bytes will appear as 1023 B
        • 1024 bytes will appear as 1.0 KiB
        • 500000 bytes will appear as 488.3 KiB
        • 1048576 bytes will appear as 1.0 MiB
        The format locale will be set to the one as obtained by Faces.getLocale().
        Parameters:
        bytes - The bytes to be formatted.
        Returns:
        The formatted bytes.
      • formatCurrency

        public static String formatCurrency​(Number number,
                                            String currencySymbol)
        Format the given number as currency with the given symbol. This is useful when you want to format numbers as currency in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertNumber> tag. The format locale will be set to the one as obtained by Faces.getLocale().
        Parameters:
        number - The number to be formatted as currency.
        currencySymbol - The currency symbol to be used.
        Returns:
        The number which is formatted as currency with the given symbol.
        Throws:
        NullPointerException - When the currency symbol is null.
      • formatNumber

        public static String formatNumber​(Number number,
                                          String pattern)
        Format the given number in the given pattern. This is useful when you want to format numbers in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertNumber> tag. The format locale will be set to the one as obtained by Faces.getLocale().
        Parameters:
        number - The number to be formatted in the given pattern.
        pattern - The pattern to format the given number in.
        Returns:
        The number which is formatted in the given pattern.
        Throws:
        NullPointerException - When the pattern is null.
      • formatNumberDefault

        public static String formatNumberDefault​(Number number)
        Format the given number in the default pattern of the default locale. This is useful when you want to format numbers in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertNumber> tag. The default locale is the one as obtained by Faces.getLocale().
        Parameters:
        number - The number to be formatted in the default pattern of the default locale.
        Returns:
        The number which is formatted in the default pattern of the default locale.
        Since:
        1.3
      • formatNumberDefaultForLocale

        public static String formatNumberDefaultForLocale​(Number number,
                                                          Object locale)
        Format the given number in the default pattern of the given locale. This is useful when you want to format numbers in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertNumber> tag. The given locale can be a Locale object or a string representation.
        Parameters:
        number - The number to be formatted in the default pattern of the given locale.
        locale - The locale to obtain the default pattern from.
        Returns:
        The number which is formatted in the default pattern of the given locale.
        Since:
        2.3
      • formatPercent

        public static String formatPercent​(Number number)
        Format the given number as percentage. This is useful when you want to format numbers as percentage in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertNumber> tag. The format locale will be set to the one as obtained by Faces.getLocale().
        Parameters:
        number - The number to be formatted as percentage.
        Returns:
        The number which is formatted as percentage.
        Since:
        1.6
      • formatThousands

        public static String formatThousands​(Number number)
        Format the given number to nearest 10n (rounded to thousands), immediately suffixed (without space) with metric unit (k, M, G, T, P or E), rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. For example (with English locale):
        • 1.6666 will appear as 1.67
        • 999.4 will appear as 999
        • 999.5 will appear as 1k
        • 1004 will appear as 1k
        • 1005 will appear as 1.01k
        • 1594 will appear as 1.59k
        • 1595 will appear as 1.6k
        • 9000 will appear as 9k
        • 9900 will appear as 9.9k
        • 9994 will appear as 9.99k
        • 9995 will appear as 10k
        • 99990 will appear as 100k
        • 9994999 will appear as 9.99M
        • 9995000 will appear as 10M
        The format locale will be set to the one as obtained by Faces.getLocale(). If the value is null, NaN or infinity, then this will return null.
        Parameters:
        number - The number to be formatted.
        Returns:
        The formatted number.
        Since:
        2.3
      • formatThousandsUnit

        public static String formatThousandsUnit​(Number number,
                                                 String unit)
        Format the given number to nearest 10n (rounded to thousands), suffixed with a space, the metric unit prefix (k, M, G, T, P or E) and the given unit, rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. For example (with English locale and unit B):
        • 1.6666 will appear as 1.67 B
        • 999.4 will appear as 999 B
        • 999.5 will appear as 1 kB
        • 1004 will appear as 1 kB
        • 1005 will appear as 1.01 kB
        • 1594 will appear as 1.59 kB
        • 1595 will appear as 1.6 kB
        • 9000 will appear as 9 kB
        • 9900 will appear as 9.9 kB
        • 9994 will appear as 9.99 kB
        • 9995 will appear as 10 kB
        • 99990 will appear as 100 kB
        • 9994999 will appear as 9.99 MB
        • 9995000 will appear as 10 MB
        The format locale will be set to the one as obtained by Faces.getLocale(). If the value is null, NaN or infinity, then this will return null.
        Parameters:
        number - The number to be formatted.
        unit - The unit used in the format. E.g. B for Bytes, W for Watt, etc. If the unit is null, then this method will behave exactly as described in formatThousands(Number).
        Returns:
        The formatted number with unit.
        Since:
        2.3