Jump to content

Number to word


BrettF
 Share

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
Link to comment
Share on other sites

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

Func validate($input)
    If Not StringLen(String($input)) And Not IsNumber($input) Then
        MsgBox(0x40, 'Alert', 'Please Enter A Number.')
    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....

Link to comment
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);
    $aDigit = StringSplit($sInput, "", 2)
    Switch $sInputlength
        Case 1
            $sOut &= Single($aDigit[0])
        Case 2
            $sOut &= Decade($aDigit[0])
            If $aDigit[0] = 1 Then
                $sOut &= Double($aDigit[1])
            Else
                $sOut &= Single($aDigit[1])
            EndIf
        Case 3
            $sOut &= Hundred($aDigit)
        Case 4
            $sOut &= Single($aDigit[0])
            $sOut &= " Thousand, "
            $aTmp[0] = $aDigit[1]
            $aTmp[1] = $aDigit[2]
            $aTmp[2] = $aDigit[3]
            $sOut &= Hundred($aTmp)
        Case 5
            $sOut &= Decade($aDigit[0])
            If $aDigit[0] = 1 Then
                $sOut &= Double($aDigit[1])
            Else
                $sOut &= Single($aDigit[1])
            EndIf
            $sOut &= " Thousand, "
            $aTmp[0] = $aDigit[2]
            $aTmp[1] = $aDigit[3]
            $aTmp[2] = $aDigit[4]
            $sOut &= Hundred($aTmp)
        Case 6
            $aTmp[0] = $aDigit[0]
            $aTmp[1] = $aDigit[1]
            $aTmp[2] = $aDigit[2]
            $sOut &= Hundred($aTmp)
            $sOut &= " Thousand, "
            $aTmp[0] = $aDigit[3]
            $aTmp[1] = $aDigit[4]
            $aTmp[2] = $aDigit[5]
            $sOut &= Hundred($aTmp)
        Case 7
            $sOut &= Single($aDigit[0])
            $sOut &= "Million, "
            $aTmp[0] = $aDigit[1]
            $aTmp[1] = $aDigit[2]
            $aTmp[2] = $aDigit[3]
            If Not Hundred($aTmp) = "" Then
                $sOut &= Hundred($aTmp)
                $sOut &= " Thousand, "
            EndIf
            $aTmp[0] = $aDigit[4]
            $aTmp[1] = $aDigit[5]
            $aTmp[2] = $aDigit[6]
            $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 &= Decade($aAry[1])
            $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

Func Decade($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   ;==>Decade

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

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...