Number Helper
The Number Helper file contains handlers that help you work with numeric data respecting country-specific number formats. It lets you specify number formats using number codes, where each code letter is preceded with a percent sign: %c3%d,%d.%d etc.
Number Code Specifiers and Separators
Supported specifiers:
Specifier | Description |
---|---|
%d | Digits |
%c | Currency symbol (€, $, CHF, ... example: %c3%d,%d.%d) |
%s | Suffix for formatted numbers as bytes, based on size (TB, GB, MB, ... example: 3%d,%d.%d%s) |
%a | Suffix for a human-readable number like 123.4 million (quadrillion, trillion, ... example: 3%d,%d.%d%a) |
3% | Amount of digits in a digits group, this specifier must precede the numeric code (example: 3%d,%d.%d) |
5% | Defines a partition threshold, that is, the minimum number of digits required for a number to be separated into groups of digits, this specifier must precede the group size specifier of the numeric code (4%, 5%, ..., example: 5%3%d'%d.%d %c) |
Supported separators:
Separator | Description |
---|---|
. , | Decimal separators |
, . ' (typewriter apostrophe) ’ (typesetter's apostrophe) (and space) | Thousands separators |
Number Code Examples:
US-English number format including a currency symbol specifier, the number is separated into groups of three digits:
%c3%d,%d.%d
European number format including a data size suffix specifier:
3%d %d,%d%s
Swiss-German (canton St. Gallen) number format including a group partition threshold specifier and a data size suffix specifier, please note that although the integral part uses an apostrophe as decimal separator, there is a different thousands separator for the decimal part (the space after the last digits specifier):
5%3%d'%d.%d %s
Note: It is not mandatory to specify a number format. The number format parameter of all functions is optional. If this parameter is empty the number code as specified in language files is used. Country-specific number codes and text generated can be found in the following language file: system/language/<your_lang>/numberLang.lc.
Loading this Helper
This helper is loaded using the following code:
rigLoadHelper "number"
Handler Reference
The following functions are available:rigFormattedNum(number, numberCode, unit)
Converts a number to a formatted number string.
Parameters
- number: must contain a number
- numberCode: (optional) must contain a number code as described above, if this parameter is empty the number code as specified in language files is used
- unit: (optional) must contain a string like "CHF", "C$", "miles"... etc.
Example:
put "987654321.1234567" into tNumToFormat
put "3%d.%d,%d " into tFormat
put rigFormattedNum(tNumToFormat, tFormat, "km") into gData["formattedNum"]
# RESULT: 987.654.321,123 456 7 km
rigFormattedNumToNum(number, decimalSeparator)
Converts a formatted number string to a number.
Parameters
- number: must contain a number
- decimalSeparator: (optional) must contain the decimal separator ("." or ",") of the number in the number parameter, if this parameter is empty the decimal separator as specified in language files is used
Example:
put "987'654'321.123 456 7" into tFormattedToNumber
put rigFormattedNumToNum(tFormattedToNumber, ".") into gData["number"]
# RESULT: 987654321.1234567
rigNumberToCurrency(number, numberCode, symbol)
Converts a number to common currency formats, like USD, EUR, GBP... etc.
Parameters
- number: must contain a number
- numberCode: (optional) must contain a number code as described above, if this parameter is empty the number code as specified in language files is used
- symbol: (optional) must contain a string like "CHF", "C$", "€"... etc., if this parameter is empty the currency symbol as specified in language files is used
Example:
put "987654321.12" into tNumber
put rigNumberToCurrency(tNumber, , ) into gData["currency"]
# RESULT: $ 987,654,321.12 (in case the US-Englsh language file is in use)
rigNumberToAmount(number, numberCode, precision)
Converts a number to a human-readable version, like 123.4 trillion for numbers up to the quadrillions.
Parameters
- number: must contain a number
- numberCode: (optional) must contain a number code as described above, if this parameter is empty the number code as specified in language files is used
- precision: (optional) must contain an integer specifying the floating point precision, if this parameter is empty a floating point precision of 1 is used
Example:
put "987654321.1234567" into tNumber
put rigNumberToAmount(tNumber, , 5) into gData["amount"]
# RESULT: 987.654 32 million (in case the Englsh language file is in use)
rigNumberToSize(number, numberCode, precision)
Formats numbers as bytes, based on size, and adds the appropriate suffix.
Parameters
- number: must contain a number
- numberCode: (optional) must contain a number code as described above, if this parameter is empty the number code as specified in language files is used
- precision: (optional) must contain an integer specifying the floating point precision, if this parameter is empty a floating point precision of 1 is used
Example:
put "987654321.1234567" into tNumber
put rigNumberToSize(tNumber, , 5) into gData["theSize"]
# RESULT: 941.900 56 MB (in case the Englsh language file is in use)