org.spiffyui.client
Class NumberFormatter

java.lang.Object
  extended by org.spiffyui.client.NumberFormatter

public final class NumberFormatter
extends java.lang.Object

This is a set of utility methods for formatting numbers in a locale-sensitive way. The logic to match formatting with locale was initially taken from jquery-numberformatter 1.2.1, which maps a country to a format. There are 4 different formatting types depending on whether the locale is like US, DE, FR or CH. Locales like US (English) use comma (,) as the number separator and period (.) as the decimal separator. Locales like DE (German) use period (.) as the number separator and comma (,) as the decimal separator. Locales like FR (French) use space ( ) as the number separator and comma (,) as the decimal separator. Locales like CH (Switzerland) use apostrophe (') as the number separator and period (.) as the decimal separator. This utility requires JSDateUtil to determine the locale. For some locales there is no country, in which case we use the language. If none are found it defaults to be like US format. The following countries and languages are supported:

 Countries:
 Arab Emirates -> "AE"
 Australia -> "AU"
 Austria -> "AT"
 Brazil -> "BR"
 Canada -> "CA"
 China -> "CN"
 Czech -> "CZ"
 Denmark -> "DK"
 Egypt -> "EG"
 Finland -> "FI"
 France  -> "FR"
 Germany -> "DE"
 Greece -> "GR"
 Great Britain -> "GB"
 Hong Kong -> "HK"
 India -> "IN"
 Israel -> "IL"
 Japan -> "JP"
 Russia -> "RS"
 South Korea -> "KR"
 Spain -> "ES"
 Sweden -> "SE"
 Switzerland -> "CH"
 Taiwan -> "TW"
 Thailand -> "TH"
 United States -> "US"
 Vietnam -> "VN"

 Languages:
 Chinese -> "zh"
 Danish -> "da"
 Dutch -> "nl"
 English -> "en"
 French -> "fr"
 German -> "de"
 Italian -> "it"
 Japanese -> "ja"
 Portuguese -> "pt"
 Russian -> "ru"
 Spanish -> "es"
 Slovak -> "sk"
 Swedish -> "sv"

 


Field Summary
static int LIKE_CH
          A constant representing locales that are like CH (Switzerland)
static int LIKE_DE
          A constant representing locales that are like DE (German)
static int LIKE_FR
          A constant representing locales that are like FR (French)
static int LIKE_US
          A constant representing locales that are like US (English)
 
Method Summary
static java.lang.String format(double number, java.lang.String pattern)
          Return the number formatted to the browser's locale.
static java.lang.String format(int wholeNumber)
          Format an integer based on the browser's locale.
static java.lang.String formatWithAbbreviation(double number)
          Format the absolute value of the number appending an abbreviation, if necessary.
protected static java.lang.String getGigaString(java.lang.String number)
          Returns the number with the localized abbreviation for giga.
protected static java.lang.String getKiloString(java.lang.String number)
          Returns the number with the localized abbreviation for kilo.
static int getLikeLocale()
          Get the constant representing the 'locale' which the browser locale is similiar to in terms of number formatting
protected static java.lang.String getMegaString(java.lang.String number)
          Returns the number with the localized abbreviation for mega.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LIKE_US

public static final int LIKE_US
A constant representing locales that are like US (English)

See Also:
Constant Field Values

LIKE_DE

public static final int LIKE_DE
A constant representing locales that are like DE (German)

See Also:
Constant Field Values

LIKE_FR

public static final int LIKE_FR
A constant representing locales that are like FR (French)

See Also:
Constant Field Values

LIKE_CH

public static final int LIKE_CH
A constant representing locales that are like CH (Switzerland)

See Also:
Constant Field Values
Method Detail

formatWithAbbreviation

public static java.lang.String formatWithAbbreviation(double number)
Format the absolute value of the number appending an abbreviation, if necessary. If 3 digits leaves as is. If 4-6 digits adds a kilo abbreviation with up to one decimal digit. If 7-9 digits adds a mega abbreviation with up to two decimal digits. If 10+ digits adds a giga abbreviation with up to three decimal digits.

Parameters:
number - as a double
Returns:
a formatted string

getKiloString

protected static java.lang.String getKiloString(java.lang.String number)
Returns the number with the localized abbreviation for kilo. Override if specialized localization string is needed

Parameters:
number - - a number formatted as a string to be inserted into the parameterized localized string
Returns:
the number with the abbreviation

getMegaString

protected static java.lang.String getMegaString(java.lang.String number)
Returns the number with the localized abbreviation for mega. Override if specialized localization string is needed

Parameters:
number - - a number formatted as a string to be inserted into the parameterized localized string
Returns:
the number with the abbreviation

getGigaString

protected static java.lang.String getGigaString(java.lang.String number)
Returns the number with the localized abbreviation for giga. Override if specialized localization string is needed

Parameters:
number - - a number formatted as a string to be inserted into the parameterized localized string
Returns:
the number with the abbreviation

format

public static java.lang.String format(double number,
                                      java.lang.String pattern)
Return the number formatted to the browser's locale. The browser's locale is determined using the localization utilities within JSDateUtil, so the date libraries must be included in order to use this.

Parameters:
number - - the number to format as a String
pattern - - a String pattern using the following syntax:
  • 0 = Digit
  • # = Digit after the decimal, zero shows as absent
  • . = Decimal separator
Valid pattern examples include:
  • 0.#
  • 0.##
  • 0.0##
Grouping separators will be placed every 3 digits before the decimal separator. Number of digits after the separator is subject to pattern specified. You will always get at least a single whole number, so specifying a 0 before the decimal is all that is necessary.
Returns:
the formatted number as a String

getLikeLocale

public static int getLikeLocale()
Get the constant representing the 'locale' which the browser locale is similiar to in terms of number formatting

Returns:
one of the following constants LIKE_US, LIKE_DE, LIKE_FR, LIKE_CH

format

public static java.lang.String format(int wholeNumber)
Format an integer based on the browser's locale. (A convenience method equivalent to formatting a number with the pattern of "0".)

Parameters:
wholeNumber - an integer to be formatted
Returns:
the formatted integer