currency-in-words
Advanced tools
Comparing version 2.0.2 to 2.1.0
const { convert } = require('../src/index') | ||
describe('Convert Indian system', () => { | ||
describe('indian-system: input should be valid', () => { | ||
it('returns "NaN" for undefined', () => { | ||
@@ -12,268 +12,17 @@ const result = convert(undefined) | ||
}) | ||
it('returns "NaN" for an invalid number(a1.01)', () => { | ||
it('returns "NaN" for an invalid number, "a1.01"', () => { | ||
const result = convert('a1.01') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(1.1a)', () => { | ||
it('returns "NaN" for an invalid number, "1.1a"', () => { | ||
const result = convert('1.1a') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "NaN" for an invalid number(random string)', () => { | ||
it('returns "NaN" for an invalid number, "random string"', () => { | ||
const result = convert('random string') | ||
expect(result).toEqual('NaN') | ||
}) | ||
it('returns "one" when given the input 1', () => { | ||
const result = convert('1') | ||
expect(result).toEqual('one') | ||
}) | ||
it('returns "nine" when given the input 9', () => { | ||
const result = convert('9') | ||
expect(result).toEqual('nine') | ||
}) | ||
it('returns "ten" when given the input 10', () => { | ||
const result = convert('10') | ||
expect(result).toEqual('ten') | ||
}) | ||
it('returns "eleven" when given the input 11', () => { | ||
const result = convert('11') | ||
expect(result).toEqual('eleven') | ||
}) | ||
it('returns "nineteen" when given the input 19', () => { | ||
const result = convert('19') | ||
expect(result).toEqual('nineteen') | ||
}) | ||
it('returns "ninety" when given the input 90', () => { | ||
const result = convert('90') | ||
expect(result).toEqual('ninety') | ||
}) | ||
it('returns "ninety nine" when given the input 99', () => { | ||
const result = convert('99') | ||
expect(result).toEqual('ninety nine') | ||
}) | ||
it('returns "one hundred" when given the input 100', () => { | ||
const result = convert('100') | ||
expect(result).toEqual('one hundred') | ||
}) | ||
it('returns "one hundred nine" when given the input 109', () => { | ||
const result = convert('109') | ||
expect(result).toEqual('one hundred nine') | ||
}) | ||
it('returns "one hundred ten" when given the input 110', () => { | ||
const result = convert('110') | ||
expect(result).toEqual('one hundred ten') | ||
}) | ||
it('returns "one hundred eleven" when given the input 111', () => { | ||
const result = convert('111') | ||
expect(result).toEqual('one hundred eleven') | ||
}) | ||
it('returns "one hundred nineteen" when given the input 119', () => { | ||
const result = convert('119') | ||
expect(result).toEqual('one hundred nineteen') | ||
}) | ||
it('returns "one hundred ninety" when given the input 190', () => { | ||
const result = convert('190') | ||
expect(result).toEqual('one hundred ninety') | ||
}) | ||
it('returns "one hundred ninety nine" when given the input 199', () => { | ||
const result = convert('199') | ||
expect(result).toEqual('one hundred ninety nine') | ||
}) | ||
it('returns "one thousand" when given the input 1000', () => { | ||
const result = convert('1000') | ||
expect(result).toEqual('one thousand') | ||
}) | ||
it('returns "one thousand one" when given the input 1001', () => { | ||
const result = convert('1001') | ||
expect(result).toEqual('one thousand one') | ||
}) | ||
it('returns "one thousand ten" when given the input 1010', () => { | ||
const result = convert('1010') | ||
expect(result).toEqual('one thousand ten') | ||
}) | ||
it('returns "one thousand one hundred ten" when given the input 1110', () => { | ||
const result = convert('1110') | ||
expect(result).toEqual('one thousand one hundred ten') | ||
}) | ||
it('returns "one thousand one hundred eleven" when given the input 1111', () => { | ||
const result = convert('1111') | ||
expect(result).toEqual('one thousand one hundred eleven') | ||
}) | ||
it('returns "one thousand one hundred ninety nine" when given the input 1199', () => { | ||
const result = convert('1199') | ||
expect(result).toEqual('one thousand one hundred ninety nine') | ||
}) | ||
it('returns "one thousand nine hundred" when given the input 1900', () => { | ||
const result = convert('1900') | ||
expect(result).toEqual('one thousand nine hundred') | ||
}) | ||
it('returns "one thousand nine hundred ninety nine" when given the input 1999', () => { | ||
const result = convert('1999') | ||
expect(result).toEqual('one thousand nine hundred ninety nine') | ||
}) | ||
it('returns "ten thousand" when given the input 10000', () => { | ||
const result = convert('10000') | ||
expect(result).toEqual('ten thousand') | ||
}) | ||
it('returns "eleven thousand one hundred nine" when given the input 11109', () => { | ||
const result = convert('11109') | ||
expect(result).toEqual('eleven thousand one hundred nine') | ||
}) | ||
it('returns "eleven thousand one hundred ten" when given the input 11110', () => { | ||
const result = convert('11110') | ||
expect(result).toEqual('eleven thousand one hundred ten') | ||
}) | ||
it('returns "eleven thousand one hundred eleven" when given the input 11111', () => { | ||
const result = convert('11111') | ||
expect(result).toEqual('eleven thousand one hundred eleven') | ||
}) | ||
it('returns "eleven thousand one hundred nineteen" when given the input 11119', () => { | ||
const result = convert('11119') | ||
expect(result).toEqual('eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "eleven thousand one hundred ninety" when given the input 11190', () => { | ||
const result = convert('11190') | ||
expect(result).toEqual('eleven thousand one hundred ninety') | ||
}) | ||
it('returns "eleven thousand one hundred ninety nine" when given the input 11199', () => { | ||
const result = convert('11199') | ||
expect(result).toEqual('eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "one lakh" when given the input 1,00,000', () => { | ||
const result = convert('100000') | ||
expect(result).toEqual('one lakh') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred nine" when given the input 1,11,109', () => { | ||
const result = convert('111109') | ||
expect(result).toEqual('one lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred ten" when given the input 1,11,110', () => { | ||
const result = convert('111110') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred eleven" when given the input 1,11,111', () => { | ||
const result = convert('111111') | ||
expect(result).toEqual('one lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred nineteen" when given the input 1,11,119', () => { | ||
const result = convert('111119') | ||
expect(result).toEqual('one lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "one lakh leven thousand one hundred ninety" when given the input 1,11,190', () => { | ||
const result = convert('111190') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199', () => { | ||
const result = convert('111199') | ||
expect(result).toEqual('one lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "ten lakh" when given the input 10,00,000', () => { | ||
const result = convert('1000000') | ||
expect(result).toEqual('ten lakh') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred nine" when given the input 10,11,109', () => { | ||
const result = convert('1011109') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred ten" when given the input 10,11,110', () => { | ||
const result = convert('1011110') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred eleven" when given the input 10,11,111', () => { | ||
const result = convert('1011111') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred nineteen" when given the input 10,11,119', () => { | ||
const result = convert('1011119') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "ten lakh leven thousand one hundred ninety" when given the input 10,11,190', () => { | ||
const result = convert('1011190') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "ten lakh eleven thousand one hundred ninety nine" when given the input 10,11,199', () => { | ||
const result = convert('1011199') | ||
expect(result).toEqual('ten lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "one crore" when given the input 1,00,00,000', () => { | ||
const result = convert('10000000') | ||
expect(result).toEqual('one crore') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred nine" when given the input 1,01,11,109', () => { | ||
const result = convert('10111109') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred ten" when given the input 1,01,11,110', () => { | ||
const result = convert('10111110') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred eleven" when given the input 1,01,11,111', () => { | ||
const result = convert('10111111') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "oone crore ne lakh eleven thousand one hundred nineteen" when given the input 1,11,119', () => { | ||
const result = convert('10111119') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "one crore one lakh leven thousand one hundred ninety" when given the input 1,11,190', () => { | ||
const result = convert('10111190') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "one crore one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199', () => { | ||
const result = convert('10111199') | ||
expect(result).toEqual('one crore one lakh eleven thousand one hundred ninety nine') | ||
}) | ||
it('returns "ten crore ten lakh" when given the input 10,00,00,000', () => { | ||
const result = convert('101000000') | ||
expect(result).toEqual('ten crore ten lakh') | ||
}) | ||
it('returns "tten crore en lakh eleven thousand one hundred nine" when given the input 10,00,11,109', () => { | ||
const result = convert('101011109') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred nine') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred ten" when given the input 10,00,11,110', () => { | ||
const result = convert('101011110') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ten') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred eleven" when given the input 10,00,11,111', () => { | ||
const result = convert('101011111') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred eleven') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred nineteen" when given the input 10,00,11,119', () => { | ||
const result = convert('101011119') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred nineteen') | ||
}) | ||
it('returns "ten crore ten lakh leven thousand one hundred ninety" when given the input 10,00,11,190', () => { | ||
const result = convert('101011190') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ninety') | ||
}) | ||
it('returns "ten crore ten lakh eleven thousand one hundred ninety nine" when given the input 10,00,11,199', () => { | ||
const result = convert('101011199') | ||
expect(result).toEqual('ten crore ten lakh eleven thousand one hundred ninety nine') | ||
}) | ||
}) | ||
/** | ||
* Logically everything should work fine with the tests above, but some random numbers, just for fool proofing.. | ||
*/ | ||
it('returns "five hundred fifty five" when given the input 555', () => { | ||
const result = convert('555') | ||
expect(result).toEqual('five hundred fifty five') | ||
}) | ||
it('returns "seventy seven thousand two hundred eighty nine" when given the input 77,289', () => { | ||
const result = convert('77289') | ||
expect(result).toEqual('seventy seven thousand two hundred eighty nine') | ||
}) | ||
it('returns "seventeen crore ninety nine lakh one hundred" when given the input 17,99,00,100', () => { | ||
const result = convert('179900100') | ||
expect(result).toEqual('seventeen crore ninety nine lakh one hundred') | ||
}) | ||
it('returns "ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine" when given the input 99,99,99,999', () => { | ||
const result = convert('999999999') | ||
expect(result).toEqual('ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine') | ||
}) | ||
/** | ||
* And few more.. | ||
*/ | ||
describe('indian-system: decimal inputs', () => { | ||
it('returns "one.one" when given the input 1.1', () => { | ||
@@ -283,2 +32,6 @@ const result = convert('1.1') | ||
}) | ||
it('returns "eleven.eleven" when given the input 11.11', () => { | ||
const result = convert('11.11') | ||
expect(result).toEqual('eleven.eleven') | ||
}) | ||
it('returns "fifty seven.ninety nine" when given the input 57.99', () => { | ||
@@ -296,2 +49,5 @@ const result = convert('57.99') | ||
}) | ||
}) | ||
describe('indian-system: special cases', () => { | ||
it('returns "zero" when given the input 000', () => { | ||
@@ -315,3 +71,41 @@ const result = convert('000') | ||
describe('Convert international system', () => { | ||
describe('indian-system: random numbers', () => { | ||
it('returns "five hundred fifty five" when given the input 555', () => { | ||
const result = convert('555') | ||
expect(result).toEqual('five hundred fifty five') | ||
}) | ||
it('returns "seventy seven thousand two hundred eighty nine" when given the input 77,289', () => { | ||
const result = convert('77289') | ||
expect(result).toEqual('seventy seven thousand two hundred eighty nine') | ||
}) | ||
it('returns "seventeen crore ninety nine lakh one hundred" when given the input 17,99,00,100', () => { | ||
const result = convert('179900100') | ||
expect(result).toEqual('seventeen crore ninety nine lakh one hundred') | ||
}) | ||
it('expects one hundred crore, when passed in 1,00,00,00,000', () => { | ||
const result = convert('1000000000') | ||
expect(result).toBe('one hundred crore') | ||
}) | ||
it('expects one lakh crore, when passed in 10,00,00,00,00,000', () => { | ||
const result = convert('1000000000000') | ||
expect(result).toBe('one lakh crore') | ||
}) | ||
it('expects ninety nine crore ninety nine lakhs ninety nine thousand nine hundred ninety nine, when passed in 99,99,99,99,99,99,99,999', | ||
() => { | ||
const result = convert('100000000000000').trim() | ||
expect(result).toBe('one crore crore') | ||
}) | ||
it('expects nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine, when passed in 9999999999999999', | ||
() => { | ||
const result = convert('999999999999999').trim() | ||
expect(result).toBe('nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine crore ninety nine lakh ninety nine thousand nine hundred ninety nine') | ||
}) | ||
it('expects ninety nine crore ninety nine lakhs ninety nine thousand nine hundred ninety nine, when passed in 99,99,99,99,99,99,99,999', | ||
() => { | ||
const input = '9999999999999999' | ||
expect(() => convert(input)).toThrow(RangeError) | ||
}) | ||
}) | ||
describe('international-system: valid inputs', () => { | ||
it('returns "NaN" for undefined', () => { | ||
@@ -337,2 +131,44 @@ const result = convert(undefined, { format: 'intl' }) | ||
}) | ||
}) | ||
describe('international-system: decimal inputs', () => { | ||
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') | ||
}) | ||
}) | ||
describe('international-system: special cases', () => { | ||
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') | ||
}) | ||
}) | ||
describe('international-system', () => { | ||
it('returns "one" when given the input 1', () => { | ||
@@ -498,37 +334,10 @@ const result = convert('1', { format: 'intl' }) | ||
}) | ||
}) | ||
it('returns "one.one" when given the input 1.1', () => { | ||
const result = convert('1.1') | ||
expect(result).toEqual('one.one', { format: 'intl' }) | ||
describe('international-system: big numbers', () => { | ||
it('returns nine hundred ninety nine trillion nine hundred ninety nine billion nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine when passed in the input 99', () => { | ||
const amount = '999999999999' | ||
const result = convert('999999999999999', { format: 'intl' }) | ||
expect(result).toEqual('nine hundred ninety nine trillion nine hundred ninety nine billion nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine') | ||
}) | ||
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": "2.0.2", | ||
"description": "Handles both indian-system and international-system. A light-weight, fast and efficient lib that converts currency or any numbers to corresponding words", | ||
"version": "2.1.0", | ||
"description": "A light-weight, fast and efficient lib that converts currency or any numbers to corresponding words", | ||
"main": "src/index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
183
README.md
# currency-in-words | ||
**Convert currency or similar number formats to respective words** | ||
___ | ||
<br> | ||
### Installation: | ||
___ | ||
<br> | ||
@@ -12,6 +13,5 @@ ```javascript | ||
``` | ||
___ | ||
### Usage: | ||
___ | ||
@@ -42,11 +42,32 @@ ```javascript | ||
> 2. Decimals are corrected to two places | ||
<br> | ||
### Precision | ||
___ | ||
Supports upto 15 digits for both indian and international systems on their integer part. A RangeError will be thrown for length beyond 15. | ||
### :rocket: Hacks | ||
eg: calling convert with 100,000,000,000,000 * 10 will throw an error | ||
```java | ||
International system: | ||
999,999,999,999,999 (1 quadrillion - 1) | ||
Indian system: | ||
99,99,99,99,99,99,999 (100 crore crore - 1) | ||
``` | ||
### :rocket: Hacks | ||
___ | ||
> Some of the features are not implemented for the sake of backward compatability between versions. Hey, but you can always find a way around.. | ||
<br> | ||
Hard time passing it as a string? cast it from the source | ||
<br> | ||
@@ -57,4 +78,5 @@ ```javascript | ||
``` | ||
<br> | ||
To eliminate commas | ||
<br> | ||
@@ -65,4 +87,5 @@ ```javascript | ||
``` | ||
<br> | ||
For some reason, if you want the decimal places not to be corrected, control it from your code | ||
<br> | ||
@@ -74,144 +97,2 @@ ```javascript | ||
``` | ||
___ | ||
**Test Case Results** | ||
#### Added few test-case results just to make your life easier.. | ||
> **Note**: Commas in the test cases are just for readability, but doesn't support in the input. | ||
<div style="display:block;padding:20px;max-height:200px;overflow:scroll;background-color: #eee;border: 1px solid #999;"> | ||
<pre> | ||
<code> | ||
Convert Indian 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 | ||
✓ 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 (1 ms) | ||
✓ 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 (1 ms) | ||
✓ returns "one hundred ninety nine" when given the input 199 | ||
✓ returns "one thousand" when given the input 1000 | ||
✓ returns "one thousand one" when given the input 1001 | ||
✓ returns "one thousand ten" when given the input 1010 | ||
✓ returns "one thousand one hundred ten" when given the input 1110 | ||
✓ returns "one thousand one hundred eleven" when given the input 1111 | ||
✓ 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 | ||
✓ returns "ten thousand" when given the input 10000 | ||
✓ returns "eleven thousand one hundred nine" when given the input 11109 | ||
✓ returns "eleven thousand one hundred ten" when given the input 11110 | ||
✓ returns "eleven thousand one hundred eleven" when given the input 11111 | ||
✓ returns "eleven thousand one hundred nineteen" when given the input 11119 (2 ms) | ||
✓ returns "eleven thousand one hundred ninety" when given the input 11190 | ||
✓ returns "eleven thousand one hundred ninety nine" when given the input 11199 | ||
✓ returns "one lakh" when given the input 1,00,000 | ||
✓ returns "one lakh eleven thousand one hundred nine" when given the input 1,11,109 | ||
✓ 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 (1 ms) | ||
✓ returns "one lakh eleven thousand one hundred nineteen" when given the input 1,11,119 | ||
✓ returns "one lakh leven thousand one hundred ninety" when given the input 1,11,190 | ||
✓ returns "one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199 | ||
✓ returns "ten lakh" when given the input 10,00,000 | ||
✓ returns "ten lakh eleven thousand one hundred nine" when given the input 10,11,109 | ||
✓ returns "ten lakh eleven thousand one hundred ten" when given the input 10,11,110 | ||
✓ returns "ten lakh eleven thousand one hundred eleven" when given the input 10,11,111 | ||
✓ returns "ten lakh eleven thousand one hundred nineteen" when given the input 10,11,119 | ||
✓ returns "ten lakh leven thousand one hundred ninety" when given the input 10,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 | ||
✓ returns "one crore one lakh eleven thousand one hundred nine" when given the input 1,01,11,109 | ||
✓ returns "one crore one lakh eleven thousand one hundred ten" when given the input 1,01,11,110 | ||
✓ returns "one crore one lakh eleven thousand one hundred eleven" when given the input 1,01,11,111 | ||
✓ returns "oone crore ne lakh eleven thousand one hundred nineteen" when given the input 1,11,119 | ||
✓ returns "one crore one lakh leven thousand one hundred ninety" when given the input 1,11,190 | ||
✓ returns "one crore one lakh eleven thousand one hundred ninety nine" when given the input 1,11,199 | ||
✓ returns "ten crore ten lakh" when given the input 10,00,00,000 | ||
✓ returns "tten crore en lakh eleven thousand one hundred nine" when given the input 10,00,11,109 | ||
✓ returns "ten crore ten lakh eleven thousand one hundred ten" when given the input 10,00,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 | ||
✓ returns "ten crore ten lakh leven thousand one hundred ninety" when given the input 10,00,11,190 | ||
✓ returns "ten crore ten lakh eleven thousand one hundred ninety nine" when given the input 10,00,11,199 | ||
✓ returns "five hundred fifty five" when given the input 555 | ||
✓ 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 (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 | ||
✓ 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 | ||
✓ 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 | ||
</code> | ||
</pre> | ||
</div> | ||
<br> |
@@ -1,2 +0,2 @@ | ||
import { convertIntl, indianFormat, handleTens } from './utils' | ||
import { convertIntl, handleTens, convertInd } from './utils' | ||
@@ -20,21 +20,9 @@ type standard = 'in' | 'intl' | ||
if (zeroCorrected.length > 15) throw new RangeError('Number exceeds the range') | ||
if (parseInt(fraction, 10) && zeroCorrected === '0') return `zero.${handleTens(twoDecimalPlaces)}` | ||
if (zeroCorrected === '0') return 'zero' | ||
let result = '' | ||
if (format === 'intl') { | ||
result = convertIntl(zeroCorrected) | ||
if (fraction && +fraction[0] !== 0) | ||
result = `${result.trim()}.${handleTens(twoDecimalPlaces)}` | ||
} else { | ||
const integerResult = (indianFormat[zeroCorrected.length] as Function)(zeroCorrected) | ||
result = integerResult | ||
if (fraction && +fraction[0] !== 0) // ignore .01 - .09 | ||
result = `${result}.${(indianFormat[twoDecimalPlaces.length] as Function)(twoDecimalPlaces)}` // limit to two decimal places.` | ||
} | ||
return result.trim() | ||
return format === 'intl' ? | ||
convertIntl(zeroCorrected, fraction, twoDecimalPlaces) : convertInd(zeroCorrected, fraction, twoDecimalPlaces) | ||
} |
import { isFirstCharZero, isSecondCharZero, handleOnes, handleTeens, handleTens } from './common' | ||
import { indianFormat, handleTenLakh, handleCrore, handleTenCrore } from './inidan-system' | ||
import { indianFormat, handleTenLakh, handleCrore, handleTenCrore, convertInd } from './inidan-system' | ||
import { convertIntl } from './international-system' | ||
@@ -15,3 +15,4 @@ | ||
handleTens, | ||
convertIntl | ||
convertIntl, | ||
convertInd | ||
} |
@@ -1,2 +0,1 @@ | ||
import { singleDigit, twoDigit } from '../lang/en' | ||
@@ -19,3 +18,4 @@ import { isFirstCharZero, isSecondCharZero, handleOnes, handleTens } from './common' | ||
8: handleCrore, | ||
9: handleTenCrore | ||
9: handleTenCrore, | ||
10: handleArabs | ||
} | ||
@@ -77,2 +77,35 @@ | ||
export { indianFormat, handleTenLakh, handleCrore, handleTenCrore } | ||
// everything greater than 9 digits | ||
function handleArabs(value: string): string { | ||
return convertInd(value).trim() | ||
} | ||
function convertInd(integer: string, fraction?: string, twoDecimalPlaces?: string):string { | ||
let result = `` | ||
if (integer.length > 9) { | ||
result = | ||
`${(indianFormat[10] as Function)(integer.substring(0, integer.length - 7))} crore ${(indianFormat[7] as Function)(integer.substring(integer.length - 7))}` | ||
return result.trim() | ||
} | ||
result = (indianFormat[integer.length] as Function)(integer) | ||
if (fraction && twoDecimalPlaces && +fraction[0] !== 0) // ignore .01 - .09 | ||
result = `${result}.${(indianFormat[twoDecimalPlaces.length] as Function)(twoDecimalPlaces)}` // limit to two decimal places.` | ||
return result.trim() | ||
} | ||
export { | ||
indianFormat, | ||
handleHundreds, | ||
handleThousands, | ||
handleTenThousands, | ||
handleLakh, | ||
handleTenLakh, | ||
handleCrore, | ||
handleTenCrore, | ||
convertInd | ||
} |
@@ -22,6 +22,5 @@ import { singleDigit } from "../lang/en" | ||
function convertIntl(value: string): string { | ||
if (parseInt(value) === 0) return 'zero' | ||
function convertIntl(integer: string, fraction: string, twoDecimalPlaces: string): string { | ||
if (parseInt(integer) === 0) return 'zero' | ||
const [integer, fraction] = value.split('.') | ||
const integerSplitted: Array<string> = integer.split('') | ||
@@ -32,10 +31,13 @@ const hundreds: string = spliceLastThree(integerSplitted) | ||
const billions: string = spliceLastThree(integerSplitted) | ||
const trillions: 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))}` | ||
`${resolveValue(trillions, 'trillion')}${resolveValue(billions, 'billion')}${resolveValue(millions, 'million')}${resolveValue(thousands, 'thousand')}${handleHundreds(hundreds)}` | ||
return result | ||
if (fraction && +fraction[0] !== 0) | ||
result = `${result.trim()}.${handleTens(twoDecimalPlaces)}` | ||
return result.trim() | ||
} | ||
export { convertIntl } | ||
export { spliceLastThree, resolveValue, handleHundreds, convertIntl } |
15
860
45583
94