# Number to word

## Recommended Posts

I need some fresh eyes on this

I got the code from here (click view source)

I'm going wrong somewhere in the translation, but I have no idea where...

MsgBox(0, "Number to words", validate(10))

Func validate(\$input)
If StringLen(\$input) = 0 And IsNumber(\$input) = 0 Then
MsgBox(0, "", "No!")
Else
convert(\$input)
EndIf
EndFunc  ;==>validate

Func convert(\$input)
\$input = String(Int(\$input))
Local \$inputlength = StringLen(\$input);
Local \$x = 0;
Local \$teen1 = "";
Local \$teen2 = "";
Local \$teen3 = "";
Local \$numName = "";
Local \$invalidNum = "";
Local \$a1 = ""; // for insertion of million, thousand, hundred
Local \$a2 = "";
Local \$a3 = "";
Local \$a4 = "";
Local \$a5 = "";
Local \$digit[\$inputlength + 1]
Local \$store[10]

For \$i = \$inputlength To 0 Step -1
\$digit[\$i] = StringMid(\$input, \$i, 1)
Next

For \$x = \$inputlength To 0 Step -1
MsgBox(0, "\$x", \$x)
Switch \$x
Case \$x = 9
\$n = d1(\$digit[\$x]);
\$store[\$x] = \$n
Case \$x = 8
If \$digit[\$x] == "1" Then
\$teen3 = "yes"
Else
\$teen3 = ""
\$n = d2(\$digit[\$x])
\$store[\$x] = \$n
EndIf
Case \$x = 7
If \$teen3 == "yes" Then
\$teen3 = ""
\$n = d3(\$digit[\$x])
Else
\$n = d1(\$digit[\$x])
\$store[\$x] = \$n
EndIf
Case \$x = 6
\$n = d1(\$digit[\$x])
\$store[\$x] = \$n
Case \$x = 5
If \$digit[\$x] == "1" Then
\$teen2 = "yes"
EndIf
\$n = d2(\$digit[\$x])
\$store[\$x] = \$n
Case \$x = 4
If \$teen2 == "yes" Then
\$teen2 = ""
\$n = d3(\$digit[\$x])
Else
\$n = d1(\$digit[\$x])
EndIf
\$store[\$x] = \$n
Case \$x = 3
\$n = d1(\$digit[\$x])
\$store[\$x] = \$n
Case \$x = 2
If \$digit[\$x] == "1" Then
\$teen1 = "yes"
EndIf
\$n = d2(\$digit[\$x])
\$store[\$x] = \$n
Case \$x = 1
If \$teen1 == "yes" Then
\$teen1 = ""
\$n = d3(\$digit[\$x])
Else
\$n = d1(\$digit[\$x])
EndIf
\$store[\$x] = \$n
EndSwitch
MsgBox(0, "", \$store[\$x])
If \$store[\$x] == "Not a Number" Then \$invalidNum = "yes"

Switch \$inputlength
Case 1
\$store[2] = "";
Case 2
\$store[3] = "";
Case 3
\$store[4] = "";
Case 4
\$store[5] = "";
Case 5
\$store[6] = "";
Case 6
\$store[7] = "";
Case 7
\$store[8] = "";
Case 8
\$store[9] = "";
EndSwitch

If \$store[9] <> "" Then \$a1 = " Hundred, "
If (\$store[9] <> "") And (\$store[8] <> "") And (\$store[7] <> "") Then \$a2 = " Million, "
If (\$store[6] <> "") Then \$a3 = " Hundred "
If (\$store[6] <> "") And (\$store[5] <> "") And (\$store[4] <> "") Then \$a4 = " Thousand, "
If (\$store[3] <> "") Then \$a5 = " Hundred "
Next

;add up text, cancel if invalid input found
If \$invalidNum == "yes" Then
\$numName = "Invalid Input"
Else
\$numName = \$store[9] & \$a1 & \$store[8] & \$store[7] & \$a2 & \$store[6] & \$a3 & \$store[5] & \$store[4] & \$a4 & \$store[3] & \$a5 & \$store[2] & \$store[1]
EndIf

If \$numName == "" Then \$numName = "Zero"
Return \$numName;

EndFunc  ;==>convert

Func d1(\$x)
Switch (\$x)
Case '0'
\$n = ""
Case '1'
\$n = " One "
Case '2'
\$n = " Two "
Case '3'
\$n = " Three "
Case '4'
\$n = " Four "
Case '5'
\$n = " Five "
Case '6'
\$n = " Six "
Case '7'
\$n = " Seven "
Case '8'
\$n = " Eight "
Case '9'
\$n = " Nine "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n;
EndFunc  ;==>d1

Func d2(\$x)
Switch (\$x)
Case '0'
\$n = ""
Case '1'
\$n = ""
Case '2'
\$n = " Twenty "
Case '3'
\$n = " Thirty "
Case '4'
\$n = " Forty "
Case '5'
\$n = " Fifty "
Case '6'
\$n = " Sixty "
Case '7'
\$n = " Seventy "
Case '8'
\$n = " Eighty "
Case '9'
\$n = " Ninety "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n;
EndFunc  ;==>d2

Func d3(\$x)
Switch (\$x)
Case '0'
\$n = " Ten "
Case '1'
\$n = " Eleven "
Case '2'
\$n = " Twelve "
Case '3'
\$n = " Thirteen "
Case '4'
\$n = " Fourteen "
Case '5'
\$n = " Fifteen "
Case '6'
\$n = " Sixteen "
Case '7'
\$n = " Seventeen "
Case '8'
\$n = " Eighteen "
Case '9'
\$n = " Nineteen "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n;
EndFunc  ;==>d3

Thanks,

Brett

Edited by BrettF

##### Share on other sites

MsgBox(0, "Number to words", validate(911))

Func validate(\$input)
If Not StringLen(String(\$input)) And Not IsNumber(\$input) Then
Else
Return convert(\$input)
EndIf
EndFunc   ;==>validate

Func d1(\$x)
; single digit terms
Switch \$x
Case '0'
\$n = ""
Case '1'
\$n = " One "
Case '2'
\$n = " Two "
Case '3'
\$n = " Three "
Case '4'
\$n = " Four "
Case '5'
\$n = " Five "
Case '6'
\$n = " Six "
Case '7'
\$n = " Seven "
Case '8'
\$n = " Eight "
Case '9'
\$n = " Nine "
Case Else
\$n = "Not a Number"
EndSwitch

Return \$n
EndFunc   ;==>d1

Func d2(\$x)
;10x digit terms
Switch \$x
Case '0'
\$n = ""
Case '1'
\$n = ""
Case '2'
\$n = " Twenty "
Case '3'
\$n = " Thirty "
Case '4'
\$n = " Forty "
Case '5'
\$n = " Fifty "
Case '6'
\$n = " Sixty "
Case '7'
\$n = " Seventy "
Case '8'
\$n = " Eighty "
Case '9'
\$n = " Ninety "
Case Else
\$n = "Not a Number"
EndSwitch

Return \$n;
EndFunc   ;==>d2

Func d3(\$x)
;teen digit terms
Switch \$x
Case '0'
\$n = " Ten "
Case '1'
\$n = " Eleven "
Case '2'
\$n = " Twelve "
Case '3'
\$n = " Thirteen "
Case '4'
\$n = " Fourteen "
Case '5'
\$n = " Fifteen "
Case '6'
\$n = " Sixteen "
Case '7'
\$n = " Seventeen "
Case '8'
\$n = " Eighteen "
Case '9'
\$n = " Nineteen "
Case Else
\$n = "Not a Number"
EndSwitch

Return \$n
EndFunc   ;==>d3

Func convert(\$input)
\$inputlength = StringLen(\$input);
\$x = 0
\$teen1 = ""
\$teen2 = ""
\$teen3 = ""
\$numName = ""
\$invalidNum = ""
\$a1 = "" ; for insertion of million, thousand, hundred
\$a2 = ""
\$a3 = ""
\$a4 = ""
\$a5 = ""

Dim \$digit[\$inputlength + 1]
\$j = 1

For \$i = \$inputlength-1 To 0 Step -1
;puts digits into array
\$digit[\$i] = StringMid(\$input, \$j,  1)
\$j += 1
Next

Dim \$store[9]

For \$i = 0 To \$inputlength-1
\$x = \$inputlength - \$i - 1
ConsoleWrite(\$digit[\$x] & @CRLF)

Select
;assign text to each digit
Case \$x = 8
\$store[\$x] = d1(\$digit[\$x])

Case \$x = 7
If \$digit[\$x] = "1" Then
\$teen3 = "yes"
Else
\$teen3 = ""
EndIf
\$store[\$x] = d2(\$digit[\$x])

Case \$x = 6
If \$teen3 = "yes" Then
\$teen3 = ""
\$store[\$x] = d3(\$digit[\$x])
Else
\$store[\$x] = d1(\$digit[\$x])
EndIf

Case \$x = 5
\$store[\$x] = d1(\$digit[\$x])

Case \$x = 4
If \$digit[\$x] = "1" Then
\$teen2 = "yes"
Else
\$teen2 = ""
EndIf
\$store[\$x] = d2(\$digit[\$x])

Case \$x = 3
If \$teen2 = "yes" Then
\$teen2 = ""
\$store[\$x] = d3(\$digit[\$x])
Else
\$store[\$x] = d1(\$digit[\$x])
EndIf

Case \$x = 2
\$store[\$x] = d1(\$digit[\$x])

Case \$x = 1
If \$digit[\$x] = "1" Then
\$teen1 = "yes"
Else
\$teen1 = ""
EndIf
\$store[\$x] = d2(\$digit[\$x])

Case \$x = 0
If \$teen1 = "yes" Then
\$teen1 = ""
\$store[\$x] = d3(\$digit[\$x])
Else
\$store[\$x] = d1(\$digit[\$x])
EndIf
EndSelect

If \$store[\$x] = "Not a Number" Then \$invalidNum = "yes"

Switch \$inputlength
Case 1
\$store[1] = ""
Case 2
\$store[2] = ""
Case 3
\$store[3] = ""
Case 4
\$store[4] = ""
Case 5
\$store[5] = ""
Case 6
\$store[6] = ""
Case 7
\$store[7] = ""
Case 8
\$store[8] = ""
EndSwitch

If \$store[8] <> "" Then
\$a1 = " Hundred, "
Else
\$a1 = ""
EndIf

If \$store[8] <> "" Or \$store[7] <> "" Or \$store[6] <> "" Then
\$a2 = " Million, "
Else
\$a2 = ""
EndIf

If \$store[5] <> "" Then
\$a3 = " Hundred "
Else
\$a3 = ""
EndIf

If \$store[5] <> "" Or \$store[4] <> "" Or \$store[3] <> "" Then
\$a4 = " Thousand, "
Else
\$a4 = ""
EndIf

If \$store[2] <> "" Then
\$a5 = " Hundred "
Else
\$a5 = ""
EndIf
Next

;add up text, cancel if invalid input found
If \$invalidNum = "yes" Then
\$numName = "Invalid Input"
Else
\$numName = \$store[8] & \$a1 & \$store[7] & \$store[6] & _
\$a2 & \$store[5] & \$a3 & \$store[4] & \$store[3] & _
\$a4 & \$store[2] & \$a5 & \$store[1] & \$store[0]
EndIf

\$store[0] = ""
\$store[1] = ""
\$store[2] = ""
\$store[3] = ""
\$store[4] = ""
\$store[5] = ""
\$store[6] = ""
\$store[7] = ""
\$store[8] = ""

If \$numName = "" Then \$numName = "Zero"
Return String(\$numName)
EndFunc   ;==>convert

lol what an ugly work around.... javascript arrays....

Thanks

##### Share on other sites

Looked at it and decided it was easier to recode like this (not extensively tested)

MsgBox(0, "Number to words", Validate(411001))
Func Validate(\$sInput)
If StringLen(\$sInput) = 0 And IsNumber(\$sInput) = 0 Then
MsgBox(0, "", "No!")
Else
Return convert(\$sInput)
EndIf
EndFunc   ;==>validate

Func convert(\$sInput)
\$sOut = ""
Local \$aTmp[3]
\$sInput = String(Int(\$sInput))
Local \$sInputlength = StringLen(\$sInput);
Switch \$sInputlength
Case 1
Case 2
Else
EndIf
Case 3
Case 4
\$sOut &= " Thousand, "
\$sOut &= Hundred(\$aTmp)
Case 5
Else
EndIf
\$sOut &= " Thousand, "
\$sOut &= Hundred(\$aTmp)
Case 6
\$sOut &= Hundred(\$aTmp)
\$sOut &= " Thousand, "
\$sOut &= Hundred(\$aTmp)
Case 7
\$sOut &= "Million, "
If Not Hundred(\$aTmp) = "" Then
\$sOut &= Hundred(\$aTmp)
\$sOut &= " Thousand, "
EndIf
\$sOut &= Hundred(\$aTmp)
Case Else
\$sOut &= "***** Exceeded Number Range *****"
EndSwitch
;If StringRight(\$sOut, 1) = "," Then \$sOut = StringTrimRight(\$sOut, 1)
Return \$sOut
EndFunc   ;==>convert

Func Hundred(\$aAry)
Local \$sTxt = ""
If Not Single(\$aAry[0]) = "" Then \$sTxt &= Single(\$aAry[0]) & " Hundred "
Switch \$aAry[1]
Case 0
\$sTxt &= Single(\$aAry[2])
Case 1
If \$aAry[2] == '0' Then
\$sTxt &= " Ten "
Else
;\$sTxt &= " and " & Double(\$aAry[2])
\$sTxt &= Double(\$aAry[2])
EndIf
Case Else
\$sTxt &= Single(\$aAry[2])
EndSwitch
Return \$sTxt
EndFunc   ;==>Hundred

Func Single(\$x)
Switch \$x
Case '0'
\$n = ""
Case '1'
\$n = " One "
Case '2'
\$n = " Two "
Case '3'
\$n = " Three "
Case '4'
\$n = " Four "
Case '5'
\$n = " Five "
Case '6'
\$n = " Six "
Case '7'
\$n = " Seven "
Case '8'
\$n = " Eight "
Case '9'
\$n = " Nine "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n
EndFunc   ;==>Single

Switch \$x
Case '0'
\$n = ""
Case '1'
\$n = ""
Case '2'
\$n = " Twenty "
Case '3'
\$n = " Thirty "
Case '4'
\$n = " Forty "
Case '5'
\$n = " Fifty "
Case '6'
\$n = " Sixty "
Case '7'
\$n = " Seventy "
Case '8'
\$n = " Eighty "
Case '9'
\$n = " Ninety "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n;

Func Double(\$x)
Switch \$x
Case '0'
\$n = " Ten "
Case '1'
\$n = " Eleven "
Case '2'
\$n = " Twelve "
Case '3'
\$n = " Thirteen "
Case '4'
\$n = " Fourteen "
Case '5'
\$n = " Fifteen "
Case '6'
\$n = " Sixteen "
Case '7'
\$n = " Seventeen "
Case '8'
\$n = " Eighteen "
Case '9'
\$n = " Nineteen "
Case Else
\$n = "Not a Number";
EndSwitch
Return \$n;
EndFunc   ;==>Double

##### Share on other sites

Seemed like a lot of unnecessary overloading there... so I thought I'd play a bit and see what a different approach would look like:

ConsoleWrite(_Num_To_String("5334293904") & @CRLF)

Func _Num_To_String(\$s_string)

If StringLen(\$s_string) > 12 Then Return SetError(1, 0, "Invalid Entry")
If StringRegExp(\$s_string, "^\d+\z") = 0 Then Return SetError(2, 0, "Invalid Entry")

Local \$a_sz_nums
If StringRegExp(\$s_string, "\d{1,3}\d{3}\d{3}\d{3}") Then
\$a_sz_nums = StringRegExp(\$s_string, "(\d{1,3})(\d{3})(\d{3})(\d{3})", 1)
ElseIf StringRegExp(\$s_string, "\d{1,3}\d{3}\d{3}") Then
\$a_sz_nums = StringRegExp(\$s_string, "(\d{1,3})(\d{3})(\d{3})", 1)
ElseIf StringRegExp(\$s_string, "\d{1,3}\d{3}") Then
\$a_sz_nums = StringRegExp(\$s_string, "(\d{1,3})(\d{3})", 1)
Else
\$a_sz_nums = StringRegExp(\$s_string, "(\d+)", 1)
EndIf

Local \$s_ret = "", \$s_convert
Switch UBound(\$a_sz_nums)
Case 0
Return SetError(3, 0, "")
Case 1
\$s_ret = _Convert_Num(\$s_string)
Case 2
\$s_convert = _Convert_Num(\$a_sz_nums[0], 1)
If \$s_convert <> "" Then \$s_ret = \$s_convert & "Thousand,"
\$s_convert = _Convert_Num(\$a_sz_nums[1], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert
Case 3
\$s_convert = _Convert_Num(\$a_sz_nums[0], 1)
If \$s_convert <> "" Then \$s_ret = \$s_convert & "Million,"
\$s_convert = _Convert_Num(\$a_sz_nums[1], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert & "Thousand,"
\$s_convert = _Convert_Num(\$a_sz_nums[2], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert
Case 4
\$s_convert = _Convert_Num(\$a_sz_nums[0], 1)
If \$s_convert <> "" Then \$s_ret = \$s_convert & "Billion,"
\$s_convert = _Convert_Num(\$a_sz_nums[1], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert & "Million,"
\$s_convert = _Convert_Num(\$a_sz_nums[2], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert & "Thousand,"
\$s_convert = _Convert_Num(\$a_sz_nums[3], 1)
If \$s_convert <> "" Then \$s_ret &= \$s_convert
EndSwitch

If \$s_ret = "" Then Return "Zero"

Return StringStripWS(\$s_ret, 7)
EndFunc

Func _Convert_Num(\$s_string, \$i_type = 0)

Local \$a_sz_0to19[20] = [ _
" Zero ", " One ", " Two ", " Three ", " Four ", _
" Five ", " Six ", " Seven ", " Eight ", " Nine ",  _
" Ten ", " Eleven ", " Twelve ", " Thirteen ", " Fourteen ", _
" Fifteen ", " Sixteen ", " Seventeen ", " Eightteen ", " Nineteen " _
]

Local \$a_sz_tens[10] = [ _
"", "", " Twenty ", " Thirty ", " Fourty ", _
" Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety " _
]

Local \$s_ret, \$a_split = StringSplit(\$s_string, "")
For \$i = 0 To \$a_split[0]
\$a_split[\$i] = Int(\$a_split[\$i])
Next

Switch \$a_split[0]
Case 1
If \$a_split[1] = 0 Then
If \$i_type = 0 Then \$s_ret = \$a_sz_0to19[\$a_split[1]]
Else
\$s_ret = \$a_sz_0to19[\$a_split[1]]
EndIf
Case 2
If \$a_split[1] > 0 And \$a_split[1] > 1 Then
\$s_ret = \$a_sz_tens[\$a_split[1]]
ElseIf \$a_split[1] > 0 Then
If \$a_split[1] = 1 Then
\$s_ret = \$a_sz_0to19[Int(\$a_split[1] & \$a_split[2])]
Else
\$s_ret = \$a_sz_0to19[\$a_split[1]]
EndIf
EndIf

If \$a_split[2] > 0 And (\$a_split[1] = 0 Or \$a_split[1] > 1) Then
\$s_ret &= \$a_sz_0to19[\$a_split[2]]
EndIf
Case 3
If \$a_split[1] > 0 Then
\$s_ret = \$a_sz_0to19[\$a_split[1]] & "Hundred"
EndIf

If \$a_split[2] > 0 And \$a_split[2] > 1 Then
\$s_ret &= \$a_sz_tens[\$a_split[2]]
ElseIf \$a_split[2] > 0 Then
If \$a_split[2] = 1 Then
\$s_ret = \$a_sz_0to19[Int(\$a_split[2] & \$a_split[1])]
Else
\$s_ret = \$a_sz_0to19[\$a_split[2]]
EndIf
EndIf

If \$a_split[3] > 0 And (\$a_split[2] = 0 Or \$a_split[2] > 1) Then
\$s_ret &= \$a_sz_0to19[\$a_split[3]]
EndIf
EndSwitch

Return \$s_ret
EndFunc

Edited by SmOke_N
Fixed logic error

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Sweet thanks guys!

##### Share on other sites

Sweet thanks guys!

If you're using mine Brett, be sure to check the edit. There was a logic error.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Sure thing.

Thank you!

Brett

## Create an account

Register a new account

×

• Wiki

• Back

• Git