How can i convert amount like 400 as Four Hundred dollar only , any one can help me pls
bdrkb 0 Newbie Poster
abu taher 34 Practically a Posting Shark
No declarations at all.
1. Create a form and add two textboxes; TextBox1 and TextBox2
2. Add a command button; CommandButton1
3. Copy the code below to the code window.
4. Run the program, enter the number in TextBox1 and click CommandButton1...
Function Getword(strNumber)
Dim strtemp As String
Dim x, y, z As Integer
x = Val(Mid$(strNumber, 1, 1))
y = Val(Mid$(strNumber, 2, 1))
z = Val(Mid$(strNumber, 3, 1))
If Len(strNumber) = 1 Then
strtemp = strtemp + Whole(Val(strNumber))
GoTo 20
ElseIf Len(strNumber) = 2 Then
If x = 1 And y = 0 Then
strtemp = strtemp + " Ten"
ElseIf x = 1 And y = 1 Then
strtemp = strtemp + " Eleven"
GoTo 20
ElseIf x = 1 And y = 2 Then
strtemp = strtemp + " Twelve"
GoTo 20
ElseIf x = 1 And y > 2 Then
strtemp = strtemp + Whole2(y) + "een"
GoTo 20
ElseIf x = 0 And y > 0 Then
strtemp = strtemp + Whole(y)
GoTo 20
ElseIf x = 0 And y = 0 Then
strtemp = ""
GoTo 20
Else
strtemp = Whole2(x) + "y"
If y > 0 Then strtemp = strtemp + Whole(y)
GoTo 20
End If
ElseIf Len(strNumber) = 3 Then
If x > 0 Then strtemp = strtemp + Whole(x) + " Hundred"
If y = 1 And z > 2 Then
strtemp = strtemp + Whole2(z) + "een"
GoTo 20
ElseIf y = 1 And z = 1 Then
strtemp = strtemp + " Eleven"
GoTo 20
ElseIf y = 1 And z = 2 Then
strtemp = strtemp + " Twelve"
GoTo 20
ElseIf y = 1 And z = 0 Then
strtemp = strtemp + " Ten"
GoTo 20
ElseIf x = 0 And y = 0 And z = 0 Then
strtemp = ""
GoTo 20
Else
End If
If y > 0 Then strtemp = strtemp + Whole2(y) + "y"
If z > 0 Then strtemp = strtemp + Whole(z)
End If
20 Getword = strtemp
End Function
Function Whole(ByVal x As Integer)
Select Case x
Case Is = 9
Whole = " Nine"
Case 8
Whole = " Eight"
Case 7
Whole = " Seven"
Case 6
Whole = " Six"
Case 5
Whole = " Five"
Case 4
Whole = " Four"
Case 3
Whole = " Three"
Case 2
Whole = " Two"
Case 1
Whole = " One"
End Select
End Function
Function Whole2(ByVal x As Integer)
Select Case x
Case Is = 9
Whole2 = " Ninet"
Case 8
Whole2 = " Eight"
Case 7
Whole2 = " Sevent"
Case 6
Whole2 = " Sixt"
Case 5
Whole2 = " Fift"
Case 4
Whole2 = " Fourt"
Case 3
Whole2 = " Thirt"
Case 2
Whole2 = " Twent"
End Select
End Function
Private Sub CommandButton1_Click()
Dim bigstrNumber(11)
Dim strAdd As String
Dim strNumber As String
Dim strtemp As String
TextBox1 = Abs(Int(TextBox1))
strNumber = TextBox1.Text
TextBox2 = ""
bigstrNumber(0) = ""
bigstrNumber(1) = " Thousand"
bigstrNumber(2) = " Million"
bigstrNumber(3) = " Billion"
bigstrNumber(4) = " Trillion"
bigstrNumber(5) = " Quadrillion"
bigstrNumber(6) = " Pentillion"
bigstrNumber(7) = " Hexillion"
bigstrNumber(8) = " Qentillion"
bigstrNumber(9) = " Octillion"
bigstrNumber(10) = " Nonillion"
bigstrNumber(11) = " Decillion"
If Len(strNumber) > 3 Then
Do While i < 36
m = m + 1
i = i + 3
If i >= Len(strNumber) Then Exit Do
Loop
c = i - Len(strNumber)
If c > 0 Then
For D = 1 To c
strAdd = strAdd + "0"
Next D
Else
End If
strNumber = strAdd + strNumber
k = 1
For i = 1 To m
TextBox2 = TextBox2 & Getword(Mid$(strNumber, k, 3))
If Mid$(strNumber, k, 3) <> "000" Then TextBox2 = TextBox2 & bigstrNumber(m - i)
k = k + 3
Next i
Else
End If
TextBox2 = Trim$(TextBox2 & Getword(strNumber))
TextBox1 = Format$(TextBox1, "###,###,###,###,###,###,###,###,###,###,###,###")
End Sub
avirooge 0 Newbie Poster
Converting Numeric Dollar Amounts into Text
This code will convert a numeric dollar amount into text for use in such applications as printing a check. I had to do this a couple of times in a previous life with a completely different operating system and programming language, but it is amazing how logic tends to be universal, regardless of language.
Beware: This code assumes a lot of things. It assumes that the input will be in a valid numeric format. So, either check the data before this routine begins, or put some kind of error handling in it. Also, it has an upper limit of $999,999.99 because many businesses want to limit the size of any one check. This should more properly be handled outside of this routine before an amount is ever passed to it.
For demonstration purposes I have put all of the code in the command button click event. Again, it may be more useful to have it as a function call or in a code library.
To use this routine, place two text boxes and one command button on a blank form. Text1 is where you enter the numeric dollar amount. You then click on the sommand button and Text2 is where the words will be displayed.
Copy and paste the following code into the form's general declarations
------------------
Private Sub Command1_Click()
'first set up two arrays to convert numbers to words
Dim BigOnes(9) As String
Dim SmallOnes(19) As String
'and populate them
BigOnes(1) = "Ten"
BigOnes(2) = "Twenty"
BigOnes(3) = "Thirty"
BigOnes(4) = "Forty"
BigOnes(5) = "Fifty"
BigOnes(6) = "Sixty"
BigOnes(7) = "Seventy"
BigOnes(8) = "Eighty"
BigOnes(9) = "Ninety"
SmallOnes(1) = "One"
SmallOnes(2) = "Two"
SmallOnes(3) = "Three"
SmallOnes(4) = "Four"
SmallOnes(5) = "Five"
SmallOnes(6) = "Six"
SmallOnes(7) = "Seven"
SmallOnes(8) = "Eight"
SmallOnes(9) = "Nine"
SmallOnes(10) = "Ten"
SmallOnes(11) = "Eleven"
SmallOnes(12) = "Twelve"
SmallOnes(13) = "Thirteen"
SmallOnes(14) = "Fourteen"
SmallOnes(15) = "Fifteen"
SmallOnes(16) = "Sixteen"
SmallOnes(17) = "Seventeen"
SmallOnes(18) = "Eighteen"
SmallOnes(19) = "Nineteen"
'format the incoming number to guarantee six digits
'to the left of the decimal point and two to the right
'and then separate the dollars from the cents
Text1.Text = Format(Text1.Text, "000000.00")
Dollars = Left(Text1.Text, 6)
Cents = Right(Text1.Text, 2)
Words = ""
'check to make sure incoming number is not too large
If Dollars > 999999 Then
Text2.Text = "Dollar amount is too large"
Exit Sub
End If
'separate the dollars into chunks
If Dollars = 0 Then
Words = "Zero"
Else
'first do the thousands
Chunk = Left(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
Words = Words & " Thousand"
End If
'do the rest of the dollars
Chunk = Right(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
End If
End If
'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Text2.Text = Words
Exit Sub
ParseChunk:
digits = Mid(Chunk, 1, 1)
If digits > 0 Then
Words = Words & " " & SmallOnes(digits) & " Hundred"
End If
digits = Mid(Chunk, 2, 2)
If digits > 19 Then
leftdigit = Mid(Chunk, 2, 1)
rightdigit = Mid(Chunk, 3, 1)
Words = Words & " " & BigOnes(leftdigit)
If rightdigit > 0 Then
Words = Words & " " & SmallOnes(rightdigit)
End If
Else
If digits > 0 Then
Words = Words & " " & SmallOnes(digits)
End If
End If
Return
End Sub
Avinash Rooge
avirooge@yahoo.com
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.