Class

INumber

INumber(str, optionsopt)

Parse a string as a number, ignoring all locale-specific formatting.

This class is different from the standard Javascript parseInt() and parseFloat() functions in that the number to be parsed can have formatting characters in it that are not supported by those two functions, and it handles numbers written in other locales properly. For example, if you pass the string "203,231.23" to the parseFloat() function in Javascript, it will return you the number 203. The INumber class will parse it correctly and the value() function will return the number 203231.23. If you pass parseFloat() the string "203.231,23" with the locale set to de-DE, it will return you 203 again. This class will return the correct number 203231.23 again.

The options object may contain any of the following properties:

  • locale - specify the locale of the string to parse. This is used to figure out what the decimal point character is. If not specified, the default locale for the app or browser is used.
  • type - specify whether this string should be interpretted as a number, currency, or percentage amount. When the number is interpretted as a currency amount, the getCurrency() method will return something useful, otherwise it will return undefined. If the number is to be interpretted as percentage amount and there is a percentage sign in the string, then the number will be returned as a fraction from the valueOf() method. If there is no percentage sign, then the number will be returned as a regular number. That is "58.3%" will be returned as the number 0.583 but "58.3" will be returned as 58.3. Valid values for this property are "number", "currency", and "percentage". Default if this is not specified is "number".
  • onLoad - a callback function to call when the locale data is fully loaded. When the onLoad option is given, this class will attempt to load any missing locale data using the ilib loader callback. When the constructor is done (even if the data is already preassembled), the onLoad function is called with the current instance as a parameter, so this callback can be used with preassembled or dynamic loading or a mix of the two.
  • sync - tell whether to load any missing locale data synchronously or asynchronously. If this option is given as "false", then the "onLoad" callback must be given, as the instance returned from this constructor will not be usable for a while.
  • loadParams - an object containing parameters to pass to the loader callback function when locale data is missing. The parameters are not interpretted or modified in any way. They are simply passed along. The object may contain any property/value pairs as long as the calling code is in agreement with the loader callback function as to what those parameters mean.

This class is named INumber ("ilib number") so as not to conflict with the built-in Javascript Number class.

Constructor

# new INumber(str, optionsopt)

Parameters:
Name Type Attributes Description
str string | number | INumber | Number | undefined

a string to parse as a number, or a number value

options Object <optional>

Options controlling how the instance should be created

View Source INumber.js, line 89

Members

number

# value

View Source INumber.js, line 155

Methods

# getCurrency() → {Currency|undefined}

If the type of this INumber instance is "currency", then the parser will attempt to figure out which currency this amount represents. The amount can be written with any of the currency signs or ISO 4217 codes that are currently recognized by ilib, and the currency signs may occur before or after the numeric portion of the string. If no currency can be recognized, then the default currency for the locale is returned. If multiple currencies can be recognized (for example if the currency sign is "$"), then this method will prefer the one for the current locale. If multiple currencies can be recognized, but none are used in the current locale, then the first currency encountered will be used. This may produce random results, though the larger currencies occur earlier in the list. For example, if the sign found in the string is "$" and that is not the sign of the currency of the current locale then the US dollar will be recognized, as it is the largest currency that uses the "$" as its sign.

View Source INumber.js, line 285

the currency instance for this amount, or undefined if this INumber object is not of type currency

Currency | undefined

# getLocale() → {Locale}

Return the locale for this formatter instance.

View Source INumber.js, line 254

the locale instance for this formatter

Locale

# toString() → {string}

Return the original string that this number instance was created with.

View Source INumber.js, line 262

the original string

string

# valueOf() → {number}

Return the value of this INumber object as a primitive number instance.

View Source INumber.js, line 293

the value of this number instance

number