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

 org.omnifaces.el.functions.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()
andof:formatThousandsUnit()
. Since:
 1.2
 Author:
 Bauke Scholtz


Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
formatBytes(Long bytes)
Format the given bytes to nearest 10^{n} 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.static String
formatNumber(Number number, String pattern)
Format the given number in the given pattern.static String
formatNumberDefault(Number number)
Format the given number in the default pattern of the default locale.static String
formatNumberDefaultForLocale(Number number, Object locale)
Format the given number in the default pattern of the given locale.static String
formatPercent(Number number)
Format the given number as percentage.static String
formatThousands(Number number)
Format the given number to nearest 10^{n} (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.static String
formatThousandsUnit(Number number, String unit)
Format the given number to nearest 10^{n} (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.



Method Detail

formatBytes
public static String formatBytes(Long bytes)
Format the given bytes to nearest 10^{n} 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
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 thetitle
attribute of an UI component, or theitemLabel
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 byFaces.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 isnull
.

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 thetitle
attribute of an UI component, or theitemLabel
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 byFaces.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 isnull
.

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 thetitle
attribute of an UI component, or theitemLabel
attribute of select item, or wherever you can't use the<f:convertNumber>
tag. The default locale is the one as obtained byFaces.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 thetitle
attribute of an UI component, or theitemLabel
attribute of select item, or wherever you can't use the<f:convertNumber>
tag. The given locale can be aLocale
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 thetitle
attribute of an UI component, or theitemLabel
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 byFaces.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 10^{n} (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
Faces.getLocale()
. If the value isnull
,NaN
or infinity, then this will returnnull
. 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 10^{n} (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 unitB
): 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
Faces.getLocale()
. If the value isnull
,NaN
or infinity, then this will returnnull
. 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 isnull
, then this method will behave exactly as described informatThousands(Number)
. Returns:
 The formatted number with unit.
 Since:
 2.3

