currency-in-words
Advanced tools
Comparing version 1.1.4 to 2.0.0
const { convert } = require('../src/index') | ||
describe('Convert', () => { | ||
describe('Convert Indian system', () => { | ||
it('returns "NaN" for undefined', () => { | ||
@@ -306,2 +306,224 @@ const result = convert(undefined) | ||
}) | ||
it('returns "zero" when given the input 0.00', () => { | ||
const result = convert('0.00') | ||
expect(result).toEqual('zero') | ||
}) | ||
}) | ||
describe('Convert international system', () => { | ||
it('returns "NaN" for undefined', () => { | ||
const result = convert(undefined, { format: 'intl' }) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for null', () => { | ||
const result = convert(null, { format: 'intl' }) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(a1.01)', () => { | ||
const result = convert('a1.01', { format: 'intl' }) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(1.1a)', () => { | ||
const result = convert('1.1a', { format: 'intl' }) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(random string)', () => { | ||
const result = convert('random string', { format: 'intl' }) | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "one" when given the input 1', () => { | ||
const result = convert('1', { format: 'intl' }) | ||
expect(result).toEqual('one') | ||
}) | ||
it('returns "nine" when given the input 9', () => { | ||
const result = convert('9', { format: 'intl' }) | ||
expect(result).toEqual('nine') | ||
}) | ||
it('returns "ten" when given the input 10', () => { | ||
const result = convert('10', { format: 'intl' }) | ||
expect(result).toEqual('ten') | ||
}) | ||
it('returns "eleven" when given the input 11', () => { | ||
const result = convert('11', { format: 'intl' }) | ||
expect(result).toEqual('eleven') | ||
}) | ||
it('returns "nineteen" when given the input 19', () => { | ||
const result = convert('19', { format: 'intl' }) | ||
expect(result).toEqual('nineteen') | ||
}) | ||
it('returns "ninety" when given the input 90', () => { | ||
const result = convert('90', { format: 'intl' }) | ||
expect(result).toEqual('ninety') | ||
}) | ||
it('returns "ninety nine" when given the input 99', () => { | ||
const result = convert('99', { format: 'intl' }) | ||
expect(result).toEqual('ninety nine') | ||
}) | ||
it('returns "one hundred" when given the input 100', () => { | ||
const result = convert('100', { format: 'intl' }) | ||
expect(result).toEqual('one hundred') | ||
}) | ||
it('returns "one hundred nine" when given the input 109', () => { | ||
const result = convert('109', { format: 'intl' }) | ||
expect(result).toEqual('one hundred nine') | ||
}) | ||
it('returns "one hundred ten" when given the input 110', () => { | ||
const result = convert('110', { format: 'intl' }) | ||
expect(result).toEqual('one hundred ten') | ||
}) | ||
it('returns "one hundred eleven" when given the input 111', () => { | ||
const result = convert('111', { format: 'intl' }) | ||
expect(result).toEqual('one hundred eleven') | ||
}) | ||
it('returns "one hundred nineteen" when given the input 119', () => { | ||
const result = convert('119', { format: 'intl' }) | ||
expect(result).toEqual('one hundred nineteen') | ||
}) | ||
it('returns "one hundred ninety" when given the input 190', () => { | ||
const result = convert('190', { format: 'intl' }) | ||
expect(result).toEqual('one hundred ninety') | ||
}) | ||
it('returns "one hundred ninety nine" when given the input 199', () => { | ||
const result = convert('199', { format: 'intl' }) | ||
expect(result).toEqual('one hundred ninety nine') | ||
}) | ||
it('returns "one thousand" when given the input 1,000', () => { | ||
const result = convert('1000', { format: 'intl' }) | ||
expect(result).toEqual('one thousand') | ||
}) | ||
it('returns "one thousand one" when given the input 1,001', () => { | ||
const result = convert('1001', { format: 'intl' }) | ||
expect(result).toEqual('one thousand one') | ||
}) | ||
it('returns "one thousand eleven" when given the input 1,011', () => { | ||
const result = convert('1011', { format: 'intl' }) | ||
expect(result).toEqual('one thousand eleven') | ||
}) | ||
it('returns "one thousand one hundred eleven" when given the input 1,111', () => { | ||
const result = convert('1111', { format: 'intl' }) | ||
expect(result).toEqual('one thousand one hundred eleven') | ||
}) | ||
it('returns "ten thousand" when given the input 10,000', () => { | ||
const result = convert('10000', { format: 'intl' }) | ||
expect(result).toEqual('ten thousand') | ||
}) | ||
it('returns "ten thousand one" when given the input 10,001', () => { | ||
const result = convert('10001', { format: 'intl' }) | ||
expect(result).toEqual('ten thousand one') | ||
}) | ||
it('returns "ten thousand eleven" when given the input 10,011', () => { | ||
const result = convert('10011', { format: 'intl' }) | ||
expect(result).toEqual('ten thousand eleven') | ||
}) | ||
it('returns "ten thousand one hundred eleven" when given the input 10,111', () => { | ||
const result = convert('10111', { format: 'intl' }) | ||
expect(result).toEqual('ten thousand one hundred eleven') | ||
}) | ||
it('returns "eleven thousand one hundred eleven" when given the input 11,111', () => { | ||
const result = convert('11111', { format: 'intl' }) | ||
expect(result).toEqual('eleven thousand one hundred eleven') | ||
}) | ||
it('returns "one hundred thousand" when given the input 100,000', () => { | ||
const result = convert('100000', { format: 'intl' }) | ||
expect(result).toEqual('one hundred thousand') | ||
}) | ||
it('returns "one million" when given the input 1,000,000', () => { | ||
const result = convert('1000000', { format: 'intl' }) | ||
expect(result).toEqual('one million') | ||
}) | ||
it('returns "one million one" when given the input 1,000,001', () => { | ||
const result = convert('1000001', { format: 'intl' }) | ||
expect(result).toEqual('one million one') | ||
}) | ||
it('returns "one million eleven" when given the input 1,000,011', () => { | ||
const result = convert('1000011', { format: 'intl' }) | ||
expect(result).toEqual('one million eleven') | ||
}) | ||
it('returns "one million one hundred" when given the input 1,000,100', () => { | ||
const result = convert('1000100', { format: 'intl' }) | ||
expect(result).toEqual('one million one hundred') | ||
}) | ||
it('returns "one million one hundred eleven" when given the input 1,000,111', () => { | ||
const result = convert('1000111', { format: 'intl' }) | ||
expect(result).toEqual('one million one hundred eleven') | ||
}) | ||
it('returns "one million one thousand" when given the input 1,001,000', () => { | ||
const result = convert('1001000', { format: 'intl' }) | ||
expect(result).toEqual('one million one thousand') | ||
}) | ||
it('returns "one million" when given the input 1,001,001', () => { | ||
const result = convert('1001001', { format: 'intl' }) | ||
expect(result).toEqual('one million one thousand one') | ||
}) | ||
it('returns "one million one thousand eleven" when given the input 1,001,001', () => { | ||
const result = convert('1001011', { format: 'intl' }) | ||
expect(result).toEqual('one million one thousand eleven') | ||
}) | ||
it('returns "one million ten thousand one hundred eleven" when given the input 10,010,111', () => { | ||
const result = convert('1010111', { format: 'intl' }) | ||
expect(result).toEqual('one million ten thousand one hundred eleven') | ||
}) | ||
it('returns "one million eleven thousand one hundred eleven" when given the input 1,011,111', () => { | ||
const result = convert('1011111', { format: 'intl' }) | ||
expect(result).toEqual('one million eleven thousand one hundred eleven') | ||
}) | ||
it('returns "one million one hundred thousand" when given the input 1,100,000', () => { | ||
const result = convert('1100000', { format: 'intl' }) | ||
expect(result).toEqual('one million one hundred thousand') | ||
}) | ||
it('returns "ten million" when given the input 10,011,111', () => { | ||
const result = convert('10000000', { format: 'intl' }) | ||
expect(result).toEqual('ten million') | ||
}) | ||
it('returns "nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine" when given the input 999,999,999', () => { | ||
const result = convert('999999999', { format: 'intl' }) | ||
expect(result).toEqual('nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine') | ||
}) | ||
it('returns "ten million" when given the input 10,000,000.99', () => { | ||
const result = convert('10000000.99', { format: 'intl' }) | ||
expect(result).toEqual('ten million.ninety nine') | ||
}) | ||
it('returns "ten million.fifty seven" when given the input 10,000,000.57', () => { | ||
const result = convert('10000000.57', { format: 'intl' }) | ||
expect(result).toEqual('ten million.fifty seven') | ||
}) | ||
it('returns "zero.ninety nine" when given the input 0.99', () => { | ||
const result = convert('0.99', { format: 'intl' }) | ||
expect(result).toEqual('zero.ninety nine') | ||
}) | ||
it('returns "one.one" when given the input 1.1', () => { | ||
const result = convert('1.1') | ||
expect(result).toEqual('one.one', { format: 'intl' }) | ||
}) | ||
it('returns "fifty seven.ninety nine" when given the input 57.99', () => { | ||
const result = convert('57.99', { format: 'intl' }) | ||
expect(result).toEqual('fifty seven.ninety nine') | ||
}) | ||
it('returns "ten" when given the input 10.01; ignore x.01 to x.09', () => { | ||
const result = convert('10.01', { format: 'intl' }) | ||
expect(result).toEqual('ten') | ||
}) | ||
it('returns "zero" when given the input 0.00', () => { | ||
const result = convert('0.00', { format: 'intl' }) | ||
expect(result).toEqual('zero') | ||
}) | ||
it('returns "zero" when given the input 000', () => { | ||
const result = convert('000', { format: 'intl' }) | ||
expect(result).toEqual('zero') | ||
}) | ||
it('returns "three hundred thirty" when given the input 00330; corrected leading zeros', () => { | ||
const result = convert('00330', { format: 'intl' }) | ||
expect(result).toEqual('three hundred thirty') | ||
}) | ||
it('returns "eighty eight.twenty" when given the input 088.200; corrected leading zeros and two decimal places', () => { | ||
const result = convert('088.200', { format: 'intl' }) | ||
expect(result).toEqual('eighty eight.twenty') | ||
}) | ||
it('returns "99" when passed in the input 99', () => { | ||
const amount = 99 | ||
const result = convert(`${amount}`, { format: 'intl' }) | ||
expect(result).toEqual('ninety nine') | ||
}) | ||
}) |
{ | ||
"name": "currency-in-words", | ||
"version": "1.1.4", | ||
"description": "convert currency or any numbers to corresponding words, eg: 1 -> \"one\", 10 -> \"ten\", 101 -> \"one hundred and one\" etc...", | ||
"version": "2.0.0", | ||
"description": "A light-weight, fast and efficient lib that converts currency or any numbers to corresponding words", | ||
"main": "src/index.js", | ||
@@ -17,6 +17,14 @@ "scripts": { | ||
"converter", | ||
"convert", | ||
"transform", | ||
"figure", | ||
"words", | ||
"number", | ||
"translate" | ||
"translate", | ||
"indian", | ||
"international", | ||
"us", | ||
"U.S", | ||
"en", | ||
"english" | ||
], | ||
@@ -23,0 +31,0 @@ "author": "sarathantony@yahoo.co.in", |
134
README.md
# currency-in-words | ||
**Convert currency or any number formats to respective words** | ||
**Convert currency or similar number formats to respective words** | ||
___ | ||
> **Note** | ||
___ | ||
> 1. Current version supports only indian system. But, subsequent versions will support international version. | ||
> 2. Commas in the test cases are just for readability, but doesn't support in the input. | ||
___ | ||
### Installation: | ||
@@ -26,13 +17,22 @@ | ||
// indian-system | ||
const result = convert('777') | ||
// or | ||
const result = convert('777', { format: 'in' }) | ||
// international-system | ||
const result = convert('777', { format: 'intl' }) | ||
``` | ||
```javascript | ||
console.log('777') // seven hundred seventy seven | ||
console.log('1022') // one thousand twenty two | ||
console.log('1,022') // NaN | ||
console.log('66.123') // sixty six.twelve | ||
console.log('777') // seven hundred seventy seven | ||
console.log('1022') // one thousand twenty two | ||
console.log('1,022') // NaN | ||
console.log('66.123') // sixty six.twelve | ||
console.log('100000') // one lakh | ||
console.log('100000', { format: 'int' }) // one hundred thousand | ||
``` | ||
> *Note* | ||
> 1. Expect ```NaN``` when passed in ',' | ||
> 1. Expect ```NaN``` when ',' is present, eg: '100,000.99' | ||
> 2. Decimals are corrected to two places | ||
@@ -42,4 +42,7 @@ | ||
> **Note** | ||
> 1. Commas in the test cases are just for readability, but doesn't support in the input. | ||
```javascript | ||
Convert | ||
Convert Indian system | ||
✓ returns "NaN" for undefined (1 ms) | ||
@@ -49,3 +52,3 @@ ✓ returns "NaN" for null | ||
✓ returns "NaN" for an invalid number(1.1a) | ||
✓ returns "NaN" for an invalid number(random string) (1 ms) | ||
✓ returns "NaN" for an invalid number(random string) | ||
✓ returns "one" when given the input 1 | ||
@@ -59,7 +62,7 @@ ✓ returns "nine" when given the input 9 | ||
✓ returns "one hundred" when given the input 100 | ||
✓ returns "one hundred nine" when given the input 109 | ||
✓ returns "one hundred nine" when given the input 109 (1 ms) | ||
✓ returns "one hundred ten" when given the input 110 | ||
✓ returns "one hundred eleven" when given the input 111 (1 ms) | ||
✓ returns "one hundred eleven" when given the input 111 | ||
✓ returns "one hundred nineteen" when given the input 119 | ||
✓ returns "one hundred ninety" when given the input 190 | ||
✓ returns "one hundred ninety" when given the input 190 (1 ms) | ||
✓ returns "one hundred ninety nine" when given the input 199 | ||
@@ -71,4 +74,4 @@ ✓ returns "one thousand" when given the input 1000 | ||
✓ returns "one thousand one hundred eleven" when given the input 1111 | ||
✓ returns "one thousand one hundred ninety nine" when given the input 1199 (1 ms) | ||
✓ returns "one thousand nine hundred" when given the input 1900 | ||
✓ returns "one thousand one hundred ninety nine" when given the input 1199 | ||
✓ returns "one thousand nine hundred" when given the input 1900 (1 ms) | ||
✓ returns "one thousand nine hundred ninety nine" when given the input 1999 | ||
@@ -79,3 +82,3 @@ ✓ returns "ten thousand" when given the input 10000 | ||
✓ returns "eleven thousand one hundred eleven" when given the input 11111 | ||
✓ returns "eleven thousand one hundred nineteen" when given the input 11119 | ||
✓ returns "eleven thousand one hundred nineteen" when given the input 11119 (2 ms) | ||
✓ returns "eleven thousand one hundred ninety" when given the input 11190 | ||
@@ -86,3 +89,3 @@ ✓ returns "eleven thousand one hundred ninety nine" when given the input 11199 | ||
✓ returns "one lakh eleven thousand one hundred ten" when given the input 1,11,110 | ||
✓ returns "one lakh eleven thousand one hundred eleven" when given the input 1,11,111 | ||
✓ returns "one lakh eleven thousand one hundred eleven" when given the input 1,11,111 (1 ms) | ||
✓ returns "one lakh eleven thousand one hundred nineteen" when given the input 1,11,119 | ||
@@ -98,3 +101,3 @@ ✓ returns "one lakh leven thousand one hundred ninety" when given the input 1,11,190 | ||
✓ returns "ten lakh eleven thousand one hundred ninety nine" when given the input 10,11,199 | ||
✓ returns "one crore" when given the input 1,00,00,000 (1 ms) | ||
✓ returns "one crore" when given the input 1,00,00,000 | ||
✓ returns "one crore one lakh eleven thousand one hundred nine" when given the input 1,01,11,109 | ||
@@ -110,3 +113,3 @@ ✓ returns "one crore one lakh eleven thousand one hundred ten" when given the input 1,01,11,110 | ||
✓ returns "ten crore ten lakh eleven thousand one hundred eleven" when given the input 10,00,11,111 | ||
✓ returns "ten crore ten lakh eleven thousand one hundred nineteen" when given the input 10,00,11,119 (1 ms) | ||
✓ returns "ten crore ten lakh eleven thousand one hundred nineteen" when given the input 10,00,11,119 | ||
✓ returns "ten crore ten lakh leven thousand one hundred ninety" when given the input 10,00,11,190 | ||
@@ -116,7 +119,7 @@ ✓ returns "ten crore ten lakh eleven thousand one hundred ninety nine" when given the input 10,00,11,199 | ||
✓ returns "seventy seven thousand two hundred eighty nine" when given the input 77,289 | ||
✓ returns "seventeen crore ninety nine lakh one hundred" when given the input 17,99,00,100 | ||
✓ returns "seventeen crore ninety nine lakh one hundred" when given the input 17,99,00,100 (1 ms) | ||
✓ returns "ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine" when given the input 99,99,99,999 | ||
✓ returns "one.one" when given the input 1.1 | ||
✓ returns "fifty seven.ninety nine" when given the input 57.99 | ||
✓ returns "ten" when given the input 10.01; ignore x.01 to x.09 (1 ms) | ||
✓ returns "ten" when given the input 10.01; ignore x.01 to x.09 | ||
✓ returns "zero" when given the input 0.00 | ||
@@ -126,10 +129,81 @@ ✓ returns "zero" when given the input 000 | ||
✓ returns "eighty eight.twenty" when given the input 088.200; corrected leading zeros and two decimal places | ||
✓ returns "zero" when given the input 0.00 | ||
Convert international system | ||
✓ returns "NaN" for undefined (1 ms) | ||
✓ returns "NaN" for null | ||
✓ returns "NaN" for an invalid number(a1.01) | ||
✓ returns "NaN" for an invalid number(1.1a) | ||
✓ returns "NaN" for an invalid number(random string) | ||
✓ returns "one" when given the input 1 | ||
✓ returns "nine" when given the input 9 | ||
✓ returns "ten" when given the input 10 | ||
✓ returns "eleven" when given the input 11 | ||
✓ returns "nineteen" when given the input 19 (1 ms) | ||
✓ returns "ninety" when given the input 90 | ||
✓ returns "ninety nine" when given the input 99 | ||
✓ returns "one hundred" when given the input 100 | ||
✓ returns "one hundred nine" when given the input 109 | ||
✓ returns "one hundred ten" when given the input 110 | ||
✓ returns "one hundred eleven" when given the input 111 | ||
✓ returns "one hundred nineteen" when given the input 119 | ||
✓ returns "one hundred ninety" when given the input 190 | ||
✓ returns "one hundred ninety nine" when given the input 199 (1 ms) | ||
✓ returns "one thousand" when given the input 1,000 | ||
✓ returns "one thousand one" when given the input 1,001 | ||
✓ returns "one thousand eleven" when given the input 1,011 | ||
✓ returns "one thousand one hundred eleven" when given the input 1,111 | ||
✓ returns "ten thousand" when given the input 10,000 | ||
✓ returns "ten thousand one" when given the input 10,001 | ||
✓ returns "ten thousand eleven" when given the input 10,011 | ||
✓ returns "ten thousand one hundred eleven" when given the input 10,111 | ||
✓ returns "eleven thousand one hundred eleven" when given the input 11,111 | ||
✓ returns "one hundred thousand" when given the input 100,000 | ||
✓ returns "one million" when given the input 1,000,000 | ||
✓ returns "one million one" when given the input 1,000,001 | ||
✓ returns "one million eleven" when given the input 1,000,011 | ||
✓ returns "one million one hundred" when given the input 1,000,100 | ||
✓ returns "one million one hundred eleven" when given the input 1,000,111 | ||
✓ returns "one million one thousand" when given the input 1,001,000 | ||
✓ returns "one million" when given the input 1,001,001 | ||
✓ returns "one million one thousand eleven" when given the input 1,001,001 | ||
✓ returns "one million ten thousand one hundred eleven" when given the input 10,010,111 | ||
✓ returns "one million eleven thousand one hundred eleven" when given the input 1,011,111 | ||
✓ returns "one million one hundred thousand" when given the input 1,100,000 (1 ms) | ||
✓ returns "ten million" when given the input 10,011,111 | ||
✓ returns "nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine" when given the input 999,999,999 | ||
✓ returns "ten million" when given the input 10,000,000.99 | ||
✓ returns "ten million.fifty seven" when given the input 10,000,000.57 | ||
✓ returns "zero.ninety nine" when given the input 0.99 | ||
✓ returns "one.one" when given the input 1.1 | ||
✓ returns "fifty seven.ninety nine" when given the input 57.99 | ||
✓ returns "ten" when given the input 10.01; ignore x.01 to x.09 | ||
✓ returns "zero" when given the input 0.00 | ||
✓ returns "zero" when given the input 000 | ||
✓ returns "three hundred thirty" when given the input 00330; corrected leading zeros | ||
✓ returns "eighty eight.twenty" when given the input 088.200; corrected leading zeros and two decimal places | ||
``` | ||
#### :rocket: Hacks | ||
#### Hacks :rocket: | ||
> Some of the features are not implemented for the sake of backward compatability between versions. Hey, but you can always find a way around.. | ||
For some reason, if you want the decimal places not to be corrected, you can control from your code level, | ||
Hard time passing it as a string? cast it from the source | ||
```javascript | ||
const amount = 0.99 | ||
const convert = convert(`${amount}`) // template-literals will take care of casting it to a number | ||
``` | ||
To eliminate commas | ||
```javascript | ||
const regex = /,/g | ||
const amount = '1,000.99' | ||
const replaced = amount.replace(regex, '') | ||
``` | ||
For some reason, if you want the decimal places not to be corrected, you can control it from your code | ||
```javascript | ||
const amount = '123.123' | ||
@@ -136,0 +210,0 @@ const [dollars, cents] = amount.split('.') |
@@ -1,18 +0,17 @@ | ||
import { handleOnes, handleTens } from './utils/common' | ||
import { | ||
handleHundreds, handleThousands, handleTenThousands, handleLakh, handleTenLakh, handleCrore, handleTenCrore | ||
} from './utils/in' | ||
import { convertIntl } from './utils/intl' | ||
import { indian } from './utils/in' | ||
import { handleTens } from './utils/common' | ||
interface valueMap { | ||
[key: number]: string | Function | ||
} | ||
type standard = 'in' | 'int' | ||
type standard = 'in' | 'intl' | ||
type language = 'en' | ||
interface params { | ||
format?: standard | ||
format?: standard, | ||
lang?: language | ||
} | ||
export function convert( | ||
value: string, { format }: params = { format: 'int' } | ||
value: string, | ||
{ format, lang }: params = { format: 'in', lang: 'en' } | ||
): string { | ||
let result = '' | ||
const regex = /^\d*(\.\d+)?$/ | ||
@@ -22,26 +21,21 @@ if (!value || !value.match(regex)) return 'NaN' | ||
const [integer, fraction] = value.split('.') | ||
const zeroCorrected = '' + parseInt(integer, 10) | ||
const zeroCorrected = `${parseInt(integer, 10)}` | ||
if (parseInt(fraction) && zeroCorrected === '0') return `zero.${handleTens(fraction.substring(0, 2))}` | ||
if (zeroCorrected === '0') return 'zero' | ||
const indian: valueMap = { | ||
0: '', | ||
1: handleOnes, | ||
2: handleTens, | ||
3: handleHundreds, | ||
4: handleThousands, | ||
5: handleTenThousands, | ||
6: handleLakh, | ||
7: handleTenLakh, | ||
8: handleCrore, | ||
9: handleTenCrore | ||
} | ||
if (format === 'intl') { | ||
result = convertIntl(zeroCorrected) | ||
const integerResult = (indian[zeroCorrected.length] as Function)(zeroCorrected) | ||
let result = integerResult | ||
if (fraction && +fraction[0] !== 0) | ||
result = `${result.trim()}.${handleTens(fraction.substring(0, 2))}` | ||
} else { | ||
const integerResult = (indian[zeroCorrected.length] as Function)(zeroCorrected) | ||
result = integerResult | ||
if (fraction && +fraction[0] !== 0) // ignore .01 - .09 | ||
result = `${result}.${(indian[fraction.substring(0, 2).length] as Function)(fraction.substring(0, 2))}` // limit to two decimal places.` | ||
if (fraction && +fraction[0] !== 0) // ignore .01 - .09 | ||
result = `${result}.${(indian[fraction.substring(0, 2).length] as Function)(fraction.substring(0, 2))}` // limit to two decimal places.` | ||
} | ||
return result.trim() | ||
} |
import { singleDigit, twoDigit } from '../lang/en' | ||
import { isFirstCharZero, isSecondCharZero, handleTens } from './common' | ||
import { isFirstCharZero, isSecondCharZero, handleOnes, handleTens } from './common' | ||
interface valueMap { | ||
[key: number]: string | Function | ||
} | ||
export const indian: valueMap = { | ||
0: '', | ||
1: handleOnes, | ||
2: handleTens, | ||
3: handleHundreds, | ||
4: handleThousands, | ||
5: handleTenThousands, | ||
6: handleLakh, | ||
7: handleTenLakh, | ||
8: handleCrore, | ||
9: handleTenCrore | ||
} | ||
export function handleHundreds(key: string): string { | ||
if (isFirstCharZero(key)) | ||
return `${handleTens(key.substring(1))}` | ||
if (key.substring(1) === '00') | ||
return `${singleDigit[+key[0]]} hundred` | ||
return `${singleDigit[+key[0]]} hundred ${handleTens(key.substring(1))}` | ||
@@ -18,3 +33,3 @@ } | ||
return `${singleDigit[+key[0]]} thousand ${handleHundreds(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} thousand ${handleHundreds(key.substring(1))}` | ||
@@ -27,3 +42,3 @@ } | ||
return `${twoDigit[+key[0]]} thousand ${handleHundreds(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} thousand ${handleHundreds(key.slice(-3))}` | ||
@@ -34,3 +49,3 @@ } | ||
return `${handleTenThousands(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} lakh ${handleTenThousands(key.substring(1))}` | ||
@@ -43,3 +58,3 @@ } | ||
return `${twoDigit[+key[0]]} lakh ${handleTenThousands(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} lakh ${handleTenThousands(key.substring(2))}` | ||
@@ -50,3 +65,3 @@ } | ||
return `${handleTenLakh(key.substring(1))}` | ||
return `${singleDigit[+key[0]]} crore ${handleTenLakh(key.substring(1))}` | ||
@@ -57,4 +72,4 @@ } | ||
return `${twoDigit[+key[0]]} crore ${handleTenLakh(key.substring(2))}` | ||
return `${handleTens(key.substring(0, 2))} crore ${handleTenLakh(key.substring(2))}` | ||
} |
@@ -1,15 +0,35 @@ | ||
export function handleHundredThousands(key: string): string { | ||
return `` | ||
import { singleDigit } from "../lang/en" | ||
import { handleTens } from "./common" | ||
function spliceLastThree(value: Array<string>): string { | ||
return value.splice(-3).join('') | ||
} | ||
export function handleMillions(key: string): string { | ||
return `` | ||
function resolveValue(value: string, placeholder: string): string { | ||
return parseInt(value) ? `${handleHundreds(value)} ${placeholder} ` : `` | ||
} | ||
export function handleTenMillions(key: string): string { | ||
return `` | ||
function handleHundreds(value: string): string { | ||
const parsedValue = `${parseInt(value)}` | ||
if (parseInt(value) === 0) return `` | ||
if (parsedValue.length === 1) return `${singleDigit[+value]}` | ||
if (parsedValue.length === 2) return `${handleTens(parsedValue)}` | ||
return `${singleDigit[+value[0]]} hundred ${handleTens(value.substring(1))}`.trim() | ||
} | ||
export function handleBillions(key: string): string { | ||
return `` | ||
export function convertIntl(value: string): string { | ||
if (parseInt(value) === 0) return 'zero' | ||
const [integer, fraction] = value.split('.') | ||
const integerSplitted: Array<string> = integer.split('') | ||
const hundreds: string = spliceLastThree(integerSplitted) | ||
const thousands: string = spliceLastThree(integerSplitted) | ||
const millions: string = spliceLastThree(integerSplitted) | ||
const billions: string = spliceLastThree(integerSplitted) | ||
let result = | ||
`${resolveValue(billions, 'billion')}${resolveValue(millions, 'million')}${resolveValue(thousands, 'thousand')}${handleHundreds(hundreds)}` | ||
if (fraction) result = `${result}.${handleTens(fraction.substring(0, 2))}` | ||
return result | ||
} | ||
export function handleHundredBillions(key: string): string { | ||
return `` | ||
} |
53898
790
202