Link to home
Start Free TrialLog in
Avatar of jana
janaFlag for United States of America

asked on

Money currency translated into word for check in VB6 and .NET 2005

We are looking for a solution or a function or routing where we can translate a currency check amount into words.  For example:

$10,953.91  in letters/words, would be:

Ten Thousand Nine Hundred fifty three with 91/100

We looking for English and Spanish...

Whats the best way in doing this?
Avatar of Alpha Au
Alpha Au
Flag of Hong Kong image

Working on this, but have to call it a night.  Will resume tomorrow.
Avatar of jana

ASKER

The sample didn't quite work, for 3,751,258.25, it starts with "billion" when supposed to be thousand and also has no cents.
Numbers.jpg
yes you are right ...no cents!!
Although, it looks like most of the work is done with that sample.  You could take that code and add into it:

1) a "shift" of 3 to the left to remove the decimal and the cents and assign this to a dollars variable.
2) take the left over cents and display it over /100 during the text output.

That's what my code does, but it's not ready yet.  May take another while to complete my code (I'm working a full-time job at the same time), but here's the start of it.  It's not near completion.
MsgBox TranslateCurrency("12.00")

Function TranslateCurrency(myAmount)
	Dim Dollars, Cents
	If InStr(myAmount, ".") Then
		Dollars = Left(myAmount, Len(myAmount)-3)
		Cents = Right(myAmount, 2)
	Else
		Dollars = myAmount
		Cents = 0
	End If
	
	For I = 1 To Len(Dollars)
		NewAmount = DigitToText(Mid(Dollars, I, 1))
	Next
	TranslateCurrency = NewAmount
End Function

Function DigitToText(Digit)
	Select Case Digit
		Case "0"
			DigitToText = "Zero"
		Case "1"
			DigitToText = "One"
		Case "2"
			DigitToText = "Two"
		Case "3"
			DigitToText = "Three"
		Case "4"
			DigitToText = "Four"
		Case "5"
			DigitToText = "Five"
		Case "6"
			DigitToText = "Six"
		Case "7"
			DigitToText = "Seven"
		Case "8"
			DigitToText = "Eight"
		Case "9"
			DigitToText = "Nine"
		Case Else
			DigitToText = -1
		End Select
End Function

Open in new window

try this ...i chekced it
 Dim oneWords As String = ",One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen"
    Dim ones() As String = oneWords.Split(",")
    Dim tenWords As String = ",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety"
    Dim tens() As String = tenWords.Split(",")

    Private Function Convert(ByVal input As String) As String
        input = input.Replace("$", "").Replace(",", "")
        If input.Length > 12 Then Return "Error in input value"
        Dim output, dollars, mills, thous, hunds, cents As String
        Dim mill, thou, hund, cent As Integer
        If input.IndexOf(".") > 0 Then
            dollars = input.Substring(0, input.IndexOf(".")).PadLeft(9, "0")
            cents = input.Substring(input.IndexOf(".") + 1).PadRight(2, "0")
            If cents = "00" Then cents = "0"
        Else
            dollars = input.PadLeft(9, "0") : cents = "0"
        End If
        mill = CType(dollars.Substring(0, 3), Integer) : mills = convertHundreds(mill)
        thou = CType(dollars.Substring(3, 3), Integer) : thous = convertHundreds(thou)
        hund = CType(dollars.Substring(6, 3), Integer) : hunds = convertHundreds(hund)
        cent = CType(cents, Integer) : cents = convertHundreds(cent)
        output = IIf(mills.Trim = "", "", mills + " Million ")
        output += IIf(thous.Trim = "", "", thous + " Thousand ")
        output += IIf(hunds.Trim = "", "", hunds)
        output = IIf(output.Length = 0, "Zero Dollars and ", output + " Dollars and ")
        output = IIf(output = "One Dollars and ", "One Dollar and ", output)
        output += IIf(cents = "", "Zero", cents) + " Cents"
        Return output
    End Function

    Private Function convertHundreds(ByVal input As Integer) As String
        Dim output As String
        If input <= 99 Then
            output = (convertTens(input))
        Else
            output = ones(Floor(input / 100))
            output += " Hundred "
            If input - Floor(input / 100) * 100 = 0 Then
                output += ""
            Else
                output += "" + convertTens(input - Floor(input / 100) * 100)
            End If
        End If
        Return output
    End Function

    Private Function convertTens(ByVal input As Integer) As String
        Dim output As String
        If input < 20 Then
            output = ones(input)
            input = 0
        Else
            output = tens(CType(Floor(input / 10), Integer))
            input -= Floor(input / 10) * 10
        End If
        output = output + IIf(ones(input).Trim = "", "", "-" + ones(input))
        Return output
    End Function
   
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        Me.Label1.Text = Convert(Me.TextBox1.Text)
    End Sub

Open in new window

Just copy and paste in form ...
input from Textbox1
output from Label1
i forgot the namespace....Sorry
Imports.System.Math
Avatar of jana

ASKER

Ok.. I paste it and run it and it doesn't work... its giving me Error      Name 'ones' is not declared.

I inserted the texbox and the label, then double click on the form and copy as suggested, am I doing something wrong?
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Did not quite understand what you mean by iittially, maybe "initially"?, but Thanx for your patience, I'm not pro in .NET, so here it goes:

To be more specific, when I create a new project and double-click on form1, I get:

     Public Class Form1

         Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
               Handles MyBase.Load

         End Sub
     End Class

I place the code (lines 1 thru 58) right after "End sub" and before " End Class"

The namespace, which I assume its Imports.System.Math, where exactly do I place it? before "End sub" ?

Like this:

     Public Class Form1

         Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
               Handles MyBase.Load
          Imports.System.Math
         End Sub
           << code copied here>>
          Imports.System.Math
     End Class

Would this be correct?
Imports.System.Math

 Public Class Form1

         Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
               Handles MyBase.Load

         End Sub
  << code copied here>>
     
     End Class
Avatar of jana

ASKER

Done... I get the following errors:

Error      1      Identifier expected.
            (on line Imports.System.Math)
Error      2      Name 'Floor' is not declared.      
            (on line If input - Floor(input / 100) * 100 = 0 Then)
            (this last error appears 6 times on all "Floor")

FYI: i'm running this in VB 2008 Express Edition

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Ok thanx

FYI: Im using 2008, Visual Basic 2008 Express Edition
Avatar of jana

ASKER

Ok I found it.... i delete the ".".... its "Imports System.Math"
try it now with a button click
did you try it?
Because you want it in pounds (English) Replace the Word Dollar with Pounds...Now in spanish You can adjust the code to your own Current Money Words
Avatar of jana

ASKER

thanx, it worked perfectly
Won't You accept a solution?
If My Solution Works For You Why Don't You Accept it ?   :)