One Million Eight Hundred and Seventy-Six Thousand Five Hundred and Forty-Three Dollars and Twenty-One Cents
And vice-versa.
I've gotten pretty close tinkiering with the following AHK script originally based on a script written by jeeswg (although it writes "and Two One Cents" instead of "and Twenty-One Cents"). But how do I write a script going the OTHER way ... where it converts words into numbers? I.e. how do I place "One Million Eight Hundred and Seventy-Six Thousand Five Hundred and Forty-Three Dollars and Twenty-One Cents" in clipboard and have it spit out "$1,876,543.21"?
Code: Select all
vNum := Clipboard ; I START BY PLACING THE NUMBERS IN CLIPBOARD
tempVar := % JEE_NumToWord(vNum)
q:: Send %tempVar% ; WHEN I PRESS "Q", IT PASTES THE WORD VERSION OF THE NUMBERS
return
;==================================================
;JEE_NumSpell
;JEE_SpellNum
JEE_NumToWord(vNum, vDoTrimFrac:=1, vAddExtraAnd:=0)
{
if !vNum
return "zero"
oArrayX := StrSplit("Thousand,Million,Billion,Trillion,Quadrillion,Quintillion,Sextillion,Septillion,Octillion,Nonillion,Decillion", ",")
(oArrayU := StrSplit("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen", ",")).RemoveAt(0)
oArrayT := StrSplit("Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety", ",")
vNum := StrReplace(vNum, "-", "", vCount)
if vCount
vOutput := "minus "
vNum := StrReplace(vNum, ",")
vNum1 := LTrim(StrSplit(vNum, ".")[1], "0")
if vDoTrimFrac
vNum2 := RTrim(StrSplit(vNum, ".")[2], "0")
else
vNum2 := StrSplit(vNum, ".")[2]
if ((vLen := StrLen(vNum1)) > 36)
return
if (Mod(vLen, 3) = 1)
vNum1 := "00" vNum1
else if (Mod(vLen, 3) = 2)
vNum1 := "0" vNum1
vMag := Ceil(vLen/3)-1
Loop, % Ceil(vLen/3)
{
oTemp := StrSplit(SubStr(vNum1, (A_Index*3)-2, 3))
vTemp := (oTemp.2 * 10) + oTemp.3
if oTemp.1
vOutput .= oArrayU[oTemp.1] " Hundred" (vTemp?" and ":" ")
else if !vMag && (vLen > 3) && vAddExtraAnd
vOutput .= "and "
if (vTemp >= 20)
vOutput .= oArrayT[oTemp.2] (oTemp.3?"-" oArrayU[oTemp.3]:" ")
else if vTemp
vOutput .= oArrayU[vTemp]
vOutput .= " " oArrayX[vMag] " "
vMag--
}
vOutput .= "Dollars "
vOutput := RTrim(vOutput, " ")
if !vNum2
return vOutput
if (vOutput = "")
vOutput := "zero"
vOutput .= " and"
Loop, Parse, vNum2
; vOutput .= "-" oArrayU[A_LoopField]
vOutput .= " " oArrayU[A_LoopField]
vOutput .= " Cents "
return RTrim(vOutput)
}
;==================================================