Constructor
new IntlNumberConverter()
- Source:
Properties:
Name | Type | Argument | Description | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
an object literal used to provide optional information to
initialize the converter.
Properties
|
Examples
Create a number converter to parse/format currencies
var converterFactory = oj.Validation.converterFactory("number");
var options = {style: "currency", currency: "USD", minimumIntegerDigits: 2};
converter = converterFactory.createConverter(options);
A number converter for percent values using a custom (CLDR) pattern
var converterFactory = oj.Validation.converterFactory("number");
var options = {pattern: '#,##0%'};
converter = converterFactory.createConverter(options);
To parse a value as percent but format it without displaying the percent character
var options = {style: 'percent', pattern: '#,##0'};
To parse a value as currency using a custom (CLDR) pattern
var options = {pattern: '¤#,##0', currency: 'USD'};
The following decimalFormat examples are in en locale. To format a value as short (default for fraction digits is based on the locale)
var options = {style:’decimal’, decimalFormat:’short’};
converter = converterFactory.createConverter(options);
converter.format(12345);--> 12.354K
To format a value as long (default for fraction digits is based on the locale):
var options = {style:’decimal’, decimalFormat:’long’};
converter = converterFactory.createConverter(options);
converter.format(12345);--> 12.345 thousand
To format a value as short with minimum fraction digits:
options = { style:’decimal’, decimalFormat:’short’,
minimumFractionDigits:4};
converter = converterFactory.createConverter(options);
converter.format(1234);--> 1.2340K
To format a value as short with maximum fraction digits:
options = { style:’decimal’, decimalFormat:’short’,
maximumFractionDigits:0};
converter = converterFactory.createConverter(options);
converter.format(12345);--> 12K
To format a value as long with minimum and maximum fraction digits:
options = { style:’decimal’, decimalFormat:’long',
minimumFractionDigits:2, maximumFractionDigits:4};
converter = converterFactory.createConverter(options);
converter.format(12000);--> 12.00 thousand
To format a value as short with minimum and maximum fraction digits:
options = { style:’decimal’, decimalFormat:’long',
minimumFractionDigits:2, maximumFractionDigits:4};
converter = converterFactory.createConverter(options);
converter.format(12345678);--> 12.345 million
decimal style default is standard:
options = { style:’decimal’, decimalFormat:’standard’};
converter = converterFactory.createConverter(options);
converter.format(12345);--> 12,345
Methods
-
format(value) → {string}
-
Formats a Number and returns the formatted string, using the options this converter was initialized with.
Parameters:
Name Type Description value
Number | number to be formatted for display - Source:
Throws:
a ConverterError both when formatting fails, or if the options provided during initialization cannot be resolved correctly.- Type
- Error
Returns:
the localized and formatted value suitable for display. When the value is formatted as a percent it's multiplied by 100.- Type
- string
-
getHint() → {String}
-
Retrieves a hint String describing the format the value is expected to be in.
- Source:
Returns:
a hint describing the format the value is expected to be in.- Type
- String
-
getOptions() → {Object}
-
Returns the options called with converter initialization.
- Source:
Returns:
an object of options.- Type
- Object
-
Init(options)
-
Initializes the number converter instance with the set options.
Parameters:
Name Type Argument Description options
Object <optional>
an object literal used to provide an optional information to initialize the converter. - Source:
-
parse(value) → {number|null}
-
Parses a string value to return a Number, using the options this converter was initialized with.
Parameters:
Name Type Description value
String | string to parse - Source:
Throws:
a ConverterError both when parsing fails, or if the options provided during initialization cannot be resolved correctly.- Type
- Error
Returns:
the parsed number or null if the value was null or an empty string. When the value is parsed as a percent its 1/100th part is returned.- Type
- number | null
-
resolvedOptions() → {Object}
-
Returns an object literal with properties reflecting the number formatting options computed based on the options parameter. If options (or pattern) is not provided, the properties will be derived from the locale defaults.
- Source:
Throws:
a oj.ConverterError when the options that the converter was initialized with are invalid.Returns:
An object literal containing the resolved values for the following options. Some of these properties may not be present, indicating that the corresponding components will not be represented in the formatted output.- locale: a String value with the language tag of the locale whose localization is used for formatting.
- style: a String value. One of the allowed values - "decimal", "currency" or "percent".
- currency: a String value. an ISO 4217 alphabetic currency code. May be present only when style is currency.
- currencyDisplay: a String value. One of the allowed values - "code", "symbol", or "name".
- numberingSystem: a String value of the numbering system used. E.g. latn
- minimumIntegerDigits: a non-negative integer Number value indicating the minimum integer digits to be used.
- minimumFractionDigits: a non-negative integer Number value indicating the minimum fraction digits to be used.
- maximumFractionDigits: a non-negative integer Number value indicating the maximum fraction digits to be used.
- useGrouping: a Boolean value indicating whether a grouping separator is used.
- Type
- Object