Created
September 19, 2021 12:45
-
-
Save nkitku/35e5fb3b2463b46f70c6f33c3c8d739c to your computer and use it in GitHub Desktop.
Number To Word in Typescript + Fastest (Hindi, Indian, International)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class N2WHindi { | |
// "शून्य" | |
private static readonly zeroTo99: string[] = | |
'|एक|दो|तीन|चार|पाँच|छः|सात|आठ|नौ|दश|ग्यारह|बारह|तेरह|चौदह|पन्द्रह|सोलह|सत्रह|अठारह|उन्नीस|बीस|इक्कीस|बाईस|तेईस|चौबीस|पच्चीस|छब्बीस|सत्ताईस|अट्ठाईस|उनतीस|तीस|इकतीस|बत्तीस|तैंतीस|चौंतीस|पैंतीस|छत्तीस|सैंतीस|अड़तीस|उनतालीस|चालीस|इकतालीस|बयालीस|तैंतालीस|चौवालीस|पैंतालीस|छियालीस|सैंतालीस|अड़तालीस|उनचास|पचास|इक्यावन|बावन|तिरपन|चौवन|पचपन|छप्पन|सत्तावन|अट्ठावन|उनसठ|साठ|इकसठ|बासठ|तिरसठ|चौंसठ|पैंसठ|छियासठ|सड़सठ|अड़सठ|उनहत्तर|सत्तर|इकहत्तर|बहत्तर|तिहत्तर|चौहत्तर|पचहत्तर|छिहत्तर|सतहत्तर|अठहत्तर|उन्यासी|अस्सी|इक्यासी|बयासी|तिरासी|चौरासी|पचासी|छियासी|सत्तासी|अट्ठासी|नवासी|नब्बे|इक्यानबे|बानबे|तिरानबे|चौरानबे|पंचानबे|छियानबे|सत्तानबे|अट्ठानबे|निन्यान्बे'.split( | |
'|' | |
); | |
private static readonly place: string[] = 'हज़ार|लाख|करोड़|अरब|खरब|नील'.split('|'); | |
public static convert(x: string): string { | |
let n: number = x.length; | |
x = n == 0 ? '00' : n == 1 || n % 2 == 0 ? '0' + x : x; | |
n = x.length; | |
let r: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
if (n > 0) { | |
const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 1)) - 48]; | |
if (v) { | |
r = v + ' सौ ' + r; | |
} | |
} | |
for (let i = 0; n > 0; i++) { | |
const v: string = N2WHindi.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
if (v) { | |
r = v + ' ' + N2WHindi.place[i] + ' ' + r; | |
} | |
} | |
return r; | |
} | |
} | |
class N2WIndian { | |
private static readonly zeroTo99: string[] = []; | |
private static readonly place: string[] = 'Thousand|Lakh|Crore|Arab|Kharab|Nil'.split('|'); | |
static { | |
const ones: string[] = | |
'|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve|Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen'.split( | |
'|' | |
); | |
const tens: string[] = '||Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety'.split('|'); | |
for (let i = 0; i < 100; i++) { | |
const t: number = Math.floor(i / 10); | |
const o: number = i % 10; | |
N2WIndian.zeroTo99.push(t < 2 ? ones[i] : tens[t] + (o ? ' ' + ones[o] : '')); | |
} | |
} | |
public static convert(x: string): string { | |
let n: number = x.length; | |
x = n == 0 ? '00' : n == 1 || n % 2 == 0 ? '0' + x : x; | |
n = x.length; | |
let r = N2WIndian.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
if (n >= 1) { | |
const v: string = N2WIndian.zeroTo99[x.charCodeAt((n -= 1)) - 48]; | |
if (v) { | |
r = v + ' Hundred ' + r; | |
} | |
} | |
for (let i = 0; n > 0; i++) { | |
const v: string = N2WIndian.zeroTo99[x.charCodeAt((n -= 2)) * 10 + x.charCodeAt(n + 1) - 528]; | |
if (v) { | |
r = v + ' ' + N2WIndian.place[i] + ' ' + r; | |
} | |
} | |
return r; | |
} | |
} | |
class N2WIntl { | |
private static readonly zeroTo999: string[] = []; | |
private static readonly place = | |
'|Thousand|Million|Billion|Trillion|Quadrillion|Quintillion|Sextillion|Septillion|Octillion|Nonillion|Decillion|Undecillion|Duodecillion|Tredecillion|Quattuordecillion|Quindecillion|Sedecillion|Septendecillion|Octodecillion|Novendecillion|Vigintillion|Unvigintillion|Duovigintillion|Tresvigintillion|Quattuorvigintillion|Quinvigintillion|Sesvigintillion|Septemvigintillion|Octovigintillion|Novemvigintillion|Trigintillion|Untrigintillion|Duotrigintillion|Trestrigintillion|Quattuortrigintillion|Quintrigintillion|Sestrigintillion|Septentrigintillion|Octotrigintillion|Noventrigintillion|Quadragintillion'.split( | |
'|' | |
); | |
static { | |
const ones = | |
'|One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven|Twelve|Thirteen|Fourteen|Fifteen|Sixteen|Seventeen|Eighteen|Nineteen'.split( | |
'|' | |
); | |
const tens = '||Twenty|Thirty|Forty|Fifty|Sixty|Seventy|Eighty|Ninety'.split('|'); | |
for (let i = 0; i < 100; i++) { | |
const t = Math.floor(i / 10); | |
const o = i % 10; | |
N2WIntl.zeroTo999[i] = t < 2 ? ones[i] : tens[t] + (o == 0 ? '' : ' ' + ones[o]); | |
} | |
for (let i = 100; i < 1000; i++) { | |
const h = Math.floor(i / 100); | |
const t = Math.floor(i / 10) % 10; | |
const o = i % 10; | |
const r = N2WIntl.zeroTo999[h] + ' Hundred'; | |
N2WIntl.zeroTo999[i] = t == 0 && o == 0 ? r : r + ' ' + N2WIntl.zeroTo999[t * 10 + o]; | |
} | |
} | |
public static convert(x: string): string { | |
let n = x.length; | |
x = n == 0 ? '000' : '0'.repeat((3 - (n % 3)) % 3) + x; | |
n = x.length; | |
let r: string = ''; | |
for (let i = 0; n > 0; i++) { | |
const v: string = | |
N2WIntl.zeroTo999[x.charCodeAt((n -= 3)) * 100 + x.charCodeAt(n + 1) * 10 + x.charCodeAt(n + 2) - 5328]; | |
if (v) { | |
r = v + ' ' + N2WIntl.place[i] + ' ' + r; | |
} | |
} | |
return r; | |
} | |
} | |
const test = () => { | |
{ | |
let n = 5000000; | |
const test: string = '1234567890'; | |
const t0 = performance.now(); | |
while (n-- > 0) { | |
N2WHindi.convert(test); | |
} | |
const t1 = performance.now(); | |
console.log('1234567890 to 5 Million times: ' + (t1 - t0) + 'ms'); | |
console.log('10^16 -1 :\n' + '9'.repeat(15) + '\n' + N2WHindi.convert('9'.repeat(15))); | |
} | |
{ | |
let n = 5000000; | |
const test: string = '1234567890'; | |
const t0 = performance.now(); | |
while (n-- > 0) { | |
N2WIndian.convert(test); | |
} | |
const t1 = performance.now(); | |
console.log('1234567890 to 5 Million times: ' + (t1 - t0) + 'ms'); | |
console.log('10^16 -1 :\n' + '9'.repeat(15) + '\n' + N2WIndian.convert('9'.repeat(15))); | |
} | |
{ | |
let n = 5000000; | |
const test: string = '1234567890'; | |
const t0 = performance.now(); | |
while (n-- > 0) { | |
N2WIntl.convert(test); | |
} | |
const t1 = performance.now(); | |
console.log('1234567890 to 5 Million times: ' + (t1 - t0) + 'ms'); | |
console.log('10^127 -1 :\n' + '9'.repeat(126) + '\n' + N2WIntl.convert('9'.repeat(126))); | |
} | |
}; | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment