Class Numbers

java.lang.Object
org.omnifaces.el.functions.Numbers

public final class Numbers extends Object

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

Since:
1.2
Author:
Bauke Scholtz
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Format the given bytes to nearest 10n in the default pattern of the default locale with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction.
    static String
    Format the given bytes to nearest 10n in the default pattern of the given locale with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction.
    static String
    formatCurrency(Number number, String currencySymbol)
    Format the given number as currency with the given symbol in the default pattern of the default locale.
    static String
    formatCurrencyForLocale(Number number, String currencySymbol, Object locale)
    Format the given number as currency with the given symbol in the default pattern of the given locale.
    static String
    formatNumber(Number number, String pattern)
    Format the given number in the given pattern.
    static String
    Format the given number in the default pattern of the default locale.
    static String
    Format the given number in the default pattern of the given locale.
    static String
    Format the given number as percentage in the default pattern of the default locale.
    static String
    Format the given number as percentage in the default pattern of the given locale.
    static String
    Format the given number to nearest 10n (rounded to thousands) in the default pattern of the default locale, 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.
    static String
    Format the given number to nearest 10n (rounded to thousands) in the default pattern of the given locale, 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.
    static String
    Format the given number to nearest 10n (rounded to thousands) in the default pattern of the default locale, 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.
    static String
    Format the given number to nearest 10n (rounded to thousands) in the default pattern of the given locale, 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.

    Methods inherited from class java.lang.Object

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

    • formatBytes

      public static String formatBytes(Long bytes)
      Format the given bytes to nearest 10n in the default pattern of the default locale with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction. For example (with English locale):
      • 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 default locale is the one as obtained by Faces.getLocale().
      Parameters:
      bytes - The bytes to be formatted in the default pattern of the default locale.
      Returns:
      The formatted bytes.
    • formatBytesForLocale

      public static String formatBytesForLocale(Long bytes, Object locale)
      Format the given bytes to nearest 10n in the default pattern of the given locale with IEC binary unit (KiB, MiB, etc) with rounding precision of 1 fraction. For example (with English locale):
      • 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 given locale can be a Locale object or a string representation.
      Parameters:
      bytes - The bytes to be formatted in the default pattern of the given locale.
      locale - The locale to obtain the default pattern from.
      Returns:
      The formatted bytes.
      Since:
      5.0
    • formatCurrency

      public static String formatCurrency(Number number, String currencySymbol)
      Format the given number as currency with the given symbol in the default pattern of the default locale. 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 in the default pattern of the default locale.
      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.
    • formatCurrencyForLocale

      public static String formatCurrencyForLocale(Number number, String currencySymbol, Object locale)
      Format the given number as currency with the given symbol in the default pattern of the given locale. 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 given locale can be a Locale object or a string representation.
      Parameters:
      number - The number to be formatted as currency in the default pattern of the given locale.
      currencySymbol - The currency symbol to be used.
      locale - The locale to obtain the default pattern from.
      Returns:
      The number which is formatted as currency with the given symbol.
      Throws:
      NullPointerException - When the currency symbol is null.
      Since:
      5.0
    • 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 in the default pattern of the default locale. 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 default locale is the one as obtained by Faces.getLocale().
      Parameters:
      number - The number to be formatted as percentage in the default pattern of the default locale.
      Returns:
      The number which is formatted as percentage.
      Since:
      1.6
    • formatPercentForLocale

      public static String formatPercentForLocale(Number number, Object locale)
      Format the given number as percentage in the default pattern of the given locale. 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 given locale can be a Locale object or a string representation.
      Parameters:
      number - The number to be formatted as percentage in the default pattern of the given locale.
      locale - The locale to obtain the default pattern from.
      Returns:
      The number which is formatted as percentage.
      Since:
      5.0
    • formatThousands

      public static String formatThousands(Number number)
      Format the given number to nearest 10n (rounded to thousands) in the default pattern of the default locale, 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 default locale is 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 in the default pattern of the default locale.
      Returns:
      The formatted number.
      Since:
      2.3
    • formatThousandsForLocale

      public static String formatThousandsForLocale(Number number, Object locale)
      Format the given number to nearest 10n (rounded to thousands) in the default pattern of the given locale, 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 given locale can be a Locale object or a string representation. If the value is null, NaN or infinity, then this will return null.
      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 formatted number.
      Since:
      5.0
    • formatThousandsUnit

      public static String formatThousandsUnit(Number number, String unit)
      Format the given number to nearest 10n (rounded to thousands) in the default pattern of the default locale, 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 default locale is 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 in the default pattern of the default locale.
      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
    • formatThousandsUnitForLocale

      public static String formatThousandsUnitForLocale(Number number, String unit, Object locale)
      Format the given number to nearest 10n (rounded to thousands) in the default pattern of the given locale, 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 given locale can be a Locale object or a string representation. If the value is null, NaN or infinity, then this will return null.
      Parameters:
      number - The number to be formatted in the default pattern of the given locale.
      unit - The unit used in the format. E.g. B for Bytes, W for Watt, etc. If the unit
      locale - The locale to obtain the default pattern from. is null, then this method will behave exactly as described in formatThousands(Number).
      Returns:
      The formatted number with unit.
      Since:
      5.0