Class ilib.Number
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 ilib.Number 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.
Depends directive: !depends numprs.js
Defined in: ilib-dyn-full.js.
Constructor Attributes | Constructor Name and Description |
---|---|
ilib.Number(str, options)
|
Method Attributes | Method Name and Description |
---|---|
If the type of this Number instance is "currency", then the parser will attempt
to figure out which currency this amount represents.
|
|
Return the locale for this formatter instance.
|
|
toString()
Return the original string that this number instance was created with.
|
|
valueOf()
Return the value of this number object as a primitive number instance.
|
- Parameters:
- {string|number|Number|ilib.Number|undefined} str
- a string to parse as a number, or a number value
- {Object=} options
- Options controlling how the instance should be created
- Returns:
- {ilib.Currency|undefined} the currency instance for this amount, or undefined if this Number object is not of type currency
- Returns:
- {ilib.Locale} the locale instance for this formatter
- Returns:
- {string} the original string
- Returns:
- {number} the value of this number instance