Jump to content

Convert Numbers into Words


Recommended Posts

<% 
ss = "one,two,three,four,five,six,seven,eight,nine" 
ds = "ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen," & _ 
    "seventeen,eighteen,nineteen" 
ts = "twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety" 
qs = ",thousand,million,billion" 
 
Function nnn2words(iNum) 
    a = split(ss,",") 
    i = iNum mod 10 
    if i > 0 then s = a(i-1) 
    ii = int(iNum mod 100)\10 
    if ii = 1 then  
        s = split(ds,",")(i) 
    elseif ((ii>1) and (ii<10)) then 
        s = split(ts,",")(ii-2) & " " & s 
    end if 
    i = (iNum \ 100) mod 10 
    if i > 0 then s = a(i-1) & " hundred " & s 
    nnn2words = s 
End Function 
 
Function num2words(iNum) 
    i = iNum 
    if i < 0 then b = true: i = i*-1 
    if i = 0 then 
        s="zero" 
    elseif i <= 2147483647 then 
        a = split(qs,",") 
        for j = 0 to 3 
            iii = i mod 1000 
            i = i \ 1000 
            if iii > 0 then s = nnn2words(iii) & _ 
                " " & a(j) & " " & s 
        next 
    else 
        s = "out of range value" 
    end if 
    if b then s = "negative " & s 
    num2words = trim(s) 
End Function

I tried but it didn't work ... :D

$ss = "one,two,three,four,five,six,seven,eight,nine" 
$ds = "ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen" 
$ts = "twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety" 
$qs = ",thousand,million,billion" 
 
Func nnn2words($iNum) 
    $a = StringSplit($ss, ",") 
    $i = Mod($iNum, 10) 
    If $i > 0 Then 
        $s = $a($i - 1)
    EndIf
    $ii = int(Mod($iNum, 100)) / 10 
    If $ii = 1 Then  
        $s = StringSplit($ds, ",")($i) 
    ElseIf (($ii > 1) And ($ii < 10)) Then 
        $s = StringSplit($ts, ",")($ii - 2) & " " & $s 
    EndIf 
    $i = Mod(($iNum / 100), 10) 
    If $i > 0 Then $s = $a($i - 1) & " hundred " & $s 
    $nnn2words = $s 
EndFunc 
 
Func num2words($iNum) 
    $i = $iNum 
    If $i < 0 Then
        $b = True
        $i = $i * -1
    EndIf
    If $i = 0 Then 
       $s = "zero" 
    ElseIf $i <= 2147483647 Then 
        $a = StringSplit($qs, ",") 
        For $j = 0 to 3 
            $iii = Mod($i, 1000) 
            $i = $i / 1000 
            If $iii > 0 Then $s = nnn2words($iii) & " " & $a($j) & " " & $s 
        Next 
    Else 
        $s = "out of range value" 
    EndIf 
    If $b Then $s = "negative " & $s 
    num2words = StringTrimLeft($s) 
EndFunc

Thanks for your help!

Link to comment
Share on other sites

Hi, just for kicks here's from 0 to 99..

Didn't have enough time to add the rest or refine it..

Dim $ones[10] = ["zero","one","two","three","four","five","six","seven","eight","nine"]
Dim $twos[10] = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
Dim $tys[10] = ["nul","nul","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]

ConsoleWrite(Number2word(99) & @LF)

Func Number2word($iNum)
    $len = StringLen($iNum)
    Switch $len
        Case 1
            For $i = 0 To 9
                If $iNum = $i Then Return $ones[$i]
            Next
        Case 2
            If StringLeft($iNum, 1) = 1 Then
                For $i = 0 To 9
                    If StringRight($iNum, 1) = $i Then Return $twos[$i]
                Next
            ElseIf StringLeft($iNum, 1) > 1 Then
                Local $nTmp
                For $i = 2 To 9
                    If StringLeft($iNum, 1) = $i Then $nTmp = $tys[$i]
                Next
                For $i = 1 To 9
                    If StringRight($iNum, 1) = $i Then $nTmp &= " " & $ones[$i]
                Next                
                Return $nTmp
            EndIf
        Case Else
            Return "Error: Only supports from 0 - 99"
    EndSwitch
EndFunc

I'm away from home for the next 4 days , so I'll have a play with it in my spare time at work.

Cheers

Edited by smashly
Link to comment
Share on other sites

Hi, just added a touch more, now does from 0 To 999

Dim $ones[10] = ["zero","one","two","three","four","five","six","seven","eight","nine"]
Dim $twos[10] = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
Dim $tys[10] = ["nul","nul","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]

ConsoleWrite(Number2word(999) & @LF)

Func Number2word($iNum)
    $len = StringLen($iNum)
    Switch $len
        Case 1
            For $i = 0 To 9
                If $iNum = $i Then Return $ones[$i]
            Next
        Case 2
            If StringLeft($iNum, 1) = 0 Then Return Number2word(StringTrimLeft($iNum, 1))
            If StringLeft($iNum, 1) = 1 Then
                For $i = 0 To 9
                    If StringRight($iNum, 1) = $i Then Return $twos[$i]
                Next
            ElseIf StringLeft($iNum, 1) > 1 Then
                Local $nTmp
                For $i = 2 To 9
                    If StringLeft($iNum, 1) = $i Then $nTmp = $tys[$i]
                Next
                For $i = 1 To 9
                    If StringRight($iNum, 1) = $i Then $nTmp &= " " & $ones[$i]
                Next                
                Return $nTmp
            EndIf
        Case 3
            Local $nTmp = Number2word(StringTrimLeft($iNum, 1))
            If $nTmp = "Zero" Then 
                $nTmp = ""
            Else
                $nTmp = " and " & $nTmp
            EndIf   
            For $i = 1 To 9
                If StringLeft($iNum, 1) = $i Then Return $ones[$i] & " hundred" & $nTmp
            Next
        Case Else
            Return "Error: Only supports from 0 - 999"
    EndSwitch
EndFunc

Cheers

Link to comment
Share on other sites

Hi, just added a touch more, now does from 0 To 999

Dim $ones[10] = ["zero","one","two","three","four","five","six","seven","eight","nine"]
Dim $twos[10] = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
Dim $tys[10] = ["nul","nul","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]

ConsoleWrite(Number2word(999) & @LF)

Func Number2word($iNum)
    $len = StringLen($iNum)
    Switch $len
        Case 1
            For $i = 0 To 9
                If $iNum = $i Then Return $ones[$i]
            Next
        Case 2
            If StringLeft($iNum, 1) = 0 Then Return Number2word(StringTrimLeft($iNum, 1))
            If StringLeft($iNum, 1) = 1 Then
                For $i = 0 To 9
                    If StringRight($iNum, 1) = $i Then Return $twos[$i]
                Next
            ElseIf StringLeft($iNum, 1) > 1 Then
                Local $nTmp
                For $i = 2 To 9
                    If StringLeft($iNum, 1) = $i Then $nTmp = $tys[$i]
                Next
                For $i = 1 To 9
                    If StringRight($iNum, 1) = $i Then $nTmp &= " " & $ones[$i]
                Next                
                Return $nTmp
            EndIf
        Case 3
            Local $nTmp = Number2word(StringTrimLeft($iNum, 1))
            If $nTmp = "Zero" Then 
                $nTmp = ""
            Else
                $nTmp = " and " & $nTmp
            EndIf   
            For $i = 1 To 9
                If StringLeft($iNum, 1) = $i Then Return $ones[$i] & " hundred" & $nTmp
            Next
        Case Else
            Return "Error: Only supports from 0 - 999"
    EndSwitch
EndFunc

Cheers

Hi smashly,

Thanks for your help :D Long long time didn't write any programs and I am new to AutoIt too. Many things to learn :D

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...