parseAmount
function parseAmount(input): | ParsedAmount | null;Defined in: format.ts:150
Reads a rupee amount the way people type it in Pakistan, and returns it in
rupees, rounded half-up to the paisa. Returns null for anything that is
not exactly one non-negative amount; it never throws.
Accepted:
- digits, with or without grouping:
150000,150,000(international) or1,50,000(Pakistani: groups of two, then three), and decimals (1,234.50,.5,5.); a grouping must be complete and consistent; - Urdu (
۱۵۰۰۰۰) and Arabic-Indic (١٥٠٠٠٠) digits, also mixed with 0-9, and the Arabic decimal and thousands separators (٫,٬); - one currency mark before or after the number:
Rs,Rs.,PKRor₨(any case), and a trailing/-(Rs. 5,000/-); - one shorthand unit after the number, with or without a space, in any
case:
k,thousand(s),hazar,ہزار(× 1,000);lakh(s),lac(s),لاکھ(× 100,000);crore(s),cr,کروڑ(× 10,000,000). Decimals work with units:1.5 lakhis 150,000 and2.25cris 22,500,000; - whitespace around and between the parts (spaces, tabs, line breaks and no-break spaces).
Rejected (null): empty input, negative or signed amounts (-500,
+500), ranges (5-10k), more than one number (500 600), two currency
marks or two units, leftover letters (500 rupees, 5kk), grouping that
could be read two ways or is broken (1,50, 1.500,00, 1,5000), and
amounts over Rs. 50 trillion (50 lakh crore), beyond which a number can no
longer hold every paisa exactly.
The arithmetic is done on the digits, so the result is exact: 1.005
rounds to 1.01 and 1.5 lakh is exactly 150000.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input |
string |
The amount as typed |
Returns
Section titled “Returns”| ParsedAmount
| null
{ value, raw }, or null if the input is not one amount
Example
Section titled “Example”parseAmount('Rs. 1,50,000/-'); // { value: 150000, raw: 'Rs. 1,50,000/-' }parseAmount('2.25cr'); // { value: 22500000, raw: '2.25cr' }parseAmount('5-10k'); // null