Code: Select all
MsgBox % Num2Text(4)
MsgBox % Num2Text(14014)
MsgBox % Num2Text(565096364935)
Num2Text(n) {
static _tens := ["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
static tens := {20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety"}
static suf := {2:" thousand",3:" million",4:" billion"}
StringReplace, n, n, `,, , All ; remove all commas
; ##### PRE-PROCESS ERROR CHECKING #####
if !RegExMatch(n,"^\d+$") ; if var is not valid
{
MsgBox, 48, Warning!, The entry is either not valid or is not a whole number.
return ""
}
if (n > 999999999999) || (n < 1) ; if var falls outside func bounds
{
MsgBox, 48, Warning!, This function will only convert numbers between 0 and 1 trillion.
return ""
}
; ##### NUMBER SEPARATION #####
tSep := []
Loop % StrLen(n) // 3
tSep.Push(SubStr(n,-2)), n := SubStr(n,1,StrLen(n)-3)
if m := Mod(StrLen(n),3)
tSep.Push(SubStr(n,1,m))
; ##### NUMBER-TO-TEXT ANALYSIS #####
out := ""
For i, c in tSep
{
c *= 1, text := ""
if !c
continue
if (c < 100)
if !_tens[c]
if !tens[c]
text := tens[SubStr(c,1,1)*10] "-" _tens[SubStr(c,0)]
else text := tens[c]
else text := _tens[c]
else if !Mod(c,100)
text := _tens[SubStr(c,1,1)] " hundred"
else if !Mod(c,10)
text := _tens[SubStr(c,1,1)] " hundred " tens[SubStr(c,-1)]
else if _tens[SubStr(c,-1)*1]
text := _tens[SubStr(c,1,1)] " hundred " _tens[SubStr(c,-1)*1]
else text := _tens[SubStr(c,1,1)] " hundred " tens[SubStr(c,2,1)*10] "-" _tens[SubStr(c,0)]
out := text suf[i] " " out
}
return out
}