Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency Strings AS Full Text 1

Status
Not open for further replies.

NeilFrank

Programmer
Mar 12, 2000
167
CA

Is there an available VB function that will take a String
formatted as Currency and return its text equivalent, eg

"$6,325.19"

would become

"six thousand three hundred and twenty five dollars and 19/100 cents"

I wrote a flow chart to do this, but before I code it, I thought I would check.

TIA

\Frank


 
I don't think there's such a function provided with VB.

BTW, I was taught not to insert "and" after "hundred". Also, "19/100 cents" is a fraction of a penny. You should either use "19 cents" or "19/100 dollars". In other words, it would be:
"six thousand three hundred twenty five dollars and 19 cents"
 

You're right, of course. I meant to write '19/100 dollars'. But '19 cents' works

And omitting the 'and' after 'hundred' does look better.

I hope you're wrong about the function not existing!

 
Private Function CurrencyToText(curValue As Currency) As String


Static Ones(10) As String
Static Teens(10) As String
Static Tens(10) As String
Static Thousands(3) As String
Dim i As Integer, nPosition As Integer
Dim nNumber As Integer, nStars As Integer
Dim bZeroValue As Boolean
Dim stResult As String, stTemp As String, stStars As String
Dim stBuffer As String


If curValue > 999999.99 Then
MsgBox "The limit of this Function is 999999.99", vbExclamation, "Danger, Danger Will Robinson"
Exit Function
End If

Ones(0) = "zero"
Ones(1) = "one"
Ones(2) = "two"
Ones(3) = "three"
Ones(4) = "four"
Ones(5) = "five"
Ones(6) = "six"
Ones(7) = "seven"
Ones(8) = "eight"
Ones(9) = "nine"
Teens(0) = "ten"
Teens(1) = "eleven"
Teens(2) = "twelve"
Teens(3) = "thirteen"
Teens(4) = "fourteen"
Teens(5) = "fifteen"
Teens(6) = "sixteen"
Teens(7) = "seventeen"
Teens(8) = "eighteen"
Teens(9) = "nineteen"
Tens(0) = ""
Tens(1) = "ten"
Tens(2) = "twenty"
Tens(3) = "thirty"
Tens(4) = "forty"
Tens(5) = "fifty"
Tens(6) = "sixty"
Tens(7) = "seventy"
Tens(8) = "eighty"
Tens(9) = "ninty"
Thousands(0) = ""
Thousands(1) = "thousand"
'Set the cents portion of the string
'stResult = "and " & Format((curValue - Int(curValue)) * 100, "00")
'Convert the dollar portion to a string
stTemp = CStr(Int(curValue))
'parse through string(Dollar ammount)


For i = Len(stTemp) To 1 Step -1
'Grab the value of this digit
nNumber = Val(Mid(stTemp, i, 1))
'Check the position(column) of this digit
'Ones, Tens, or Hundereds
nPosition = (Len(stTemp) - i) + 1


Select Case (nPosition Mod 3)
Case 1 'Ones position
bZeroValue = False


If i = 1 Then
stBuffer = Ones(nNumber) & " "
ElseIf Mid(stTemp, i - 1, 1) = "1" Then
stBuffer = Teens(nNumber) & " "
i = i - 1 'Skip tens position
ElseIf nNumber > 0 Then
stBuffer = Ones(nNumber) & " "
Else
bZeroValue = True


If i > 1 Then


If Mid(stTemp, i - 1, 1) <> &quot;0&quot; Then
bZeroValue = False
End If

End If



If i > 2 Then


If Mid(stTemp, i - 2, 1) <> &quot;0&quot; Then
bZeroValue = False
End If

End If

stBuffer = &quot;&quot;
End If



If bZeroValue = False And nPosition > 1 Then
stBuffer = stBuffer &amp; Thousands(nPosition / 3) &amp; &quot; &quot;
End If

stResult = stBuffer &amp; stResult
Case 2 'Tens position
'Numbers like twenty-five need to be hyphenated. So......
'Check if the digit has a value other than 0 AND check the next
'digit to see if it has a value other than 0
'if both are true add the hyphen


If nNumber > 0 And Val(Mid(stTemp, i + 1, 1)) = 0 Then
stResult = Tens(nNumber) &amp; &quot; &quot; &amp; stResult
ElseIf nNumber > 0 And Val(Mid(stTemp, i + 1, 1)) > 0 Then
stResult = Tens(nNumber) &amp; &quot;-&quot; &amp; stResult
End If

Case 0 'Hundreds position


If nNumber > 0 Then
stResult = Ones(nNumber) &amp; &quot; hundred &quot; &amp; stResult
End If

End Select

Next i



If Len(stResult) > 0 Then
stResult = UCase(Left(stResult, 1)) &amp; Mid(stResult, 2)
End If

nStars = 125 - Len(stResult)


For i = 0 To nStars
stStars = stStars
Next

CurrencyToText = stResult &amp; &quot; &quot; &amp; stStars
End Function

Two textbox (text1 with the value )

Private Sub Command1_Click()
CurrencyToText (Text1)
Text2 = CurrencyToText(Text1)

End Sub
Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
In math speak, &quot;and&quot; is what is used for the decimal point.

1.1 in English is; one and one-tenth.

Therefore, it is not correct to state that 125.15 is;
one-hundred and twenty-five. The &quot;and&quot; is used to mark the decimal point. So, it's one-hundred twenty-five and 15 one-hundreths.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top