|
|
Title | Convert a dollar value into words in Visual Basic .NET |
Description | This example shows how to convert a dollar value into words in Visual Basic .NET. |
Keywords | convert number, convert dollars, dollar, dollar value, cents, number, words, hundred, thousand, million, billion, trillion, quadrillion |
Categories | Algorithms, VB.NET |
|
|
Function GroupToWords returns a string representing a number between 1 and 999. If the number is 0, it simply returns "zero."
Next if the number is greater than 99, the function gets the hundreds digit and uses the one_to_nineteen array to look up its string representation. It adds that representation plus the word "hundred" to the result. If the number has no other digits, the function returns what it has so far.
Next if the remaining number is less than 20, the function uses the one_to_nineteen array to look up the number's representation.
If the number is greater than 19, the function gets the tens digit and uses the multiples_of_ten array to get its representation. It then uses the one_to_nineteen array to add on the representation of the ones digit.
|
|
' Convert a number between 0 and 999 into words.
Private Function GroupToWords(ByVal num As Integer) As _
String
Static one_to_nineteen() As String = {"zero", "one", _
"two", "three", "four", "five", "six", "seven", _
"eight", "nine", "ten", "eleven", "twelve", _
"thirteen", "fourteen", "fifteen", "sixteen", _
"seventeen", "eightteen", "nineteen"}
Static multiples_of_ten() As String = {"twenty", _
"thirty", "forty", "fifty", "sixty", "seventy", _
"eighty", "ninety"}
' If the number is 0, return an empty string.
If num = 0 Then Return ""
' Handle the hundreds digit.
Dim digit As Integer
Dim result As String = ""
If num > 99 Then
digit = num \ 100
num = num Mod 100
result = one_to_nineteen(digit) & " hundred"
End If
' If num = 0, we have hundreds only.
If num = 0 Then Return result.Trim()
' See if the rest is less than 20.
If num < 20 Then
' Look up the correct name.
result &= " " & one_to_nineteen(num)
Else
' Handle the tens digit.
digit = num \ 10
num = num Mod 10
result &= " " & multiples_of_ten(digit - 2)
' Handle the final digit.
If num > 0 Then
result &= " " & one_to_nineteen(num)
End If
End If
Return result.Trim()
End Function
|
|
Function NumberToString returns a string representation of a longer whole number. If the number is 0, it simply returns "zero."
The function enters a loop to process each group of three digits: ones, thousands, millions, and so forth. Each time through the loop, the function gets the next group's value. It calls function GroupToWords to get a string representation of the group and adds the appropriate group name after it (e.g. thousands).
|
|
' Return a word representation of the whole number value.
Private Function NumberToString(ByVal num As Double) As _
String
' Remove any fractional part.
num = Int(num)
' If the number is 0, return zero.
If num = 0 Then Return "zero"
Static groups() As String = {"", "thousand", "million", _
"billion", "trillion", "quadrillion", "?", "??", _
"???", "????"}
Dim result As String = ""
' Process the groups, smallest first.
Dim quotient As Double
Dim remainder As Integer
Dim group_num As Integer = 0
Do While num > 0
' Get the next group of three digits.
quotient = Int(num / 1000)
remainder = CInt(num - quotient * 1000)
num = quotient
' Convert the group into words.
result = GroupToWords(remainder) & _
" " & groups(group_num) & ", " & _
result
' Get ready for the next group.
group_num += 1
Loop
' Remove the trailing ", ".
If result.EndsWith(", ") Then
result = result.Substring(0, result.Length - 2)
End If
Return result.Trim()
End Function
|
|
Function DollarsToString returns a string representation of a dollar amount in dollars and cents. It breaks the dollar value into a whole number part and an integer cents part. It calls function NumberToString to convert the dollars and cents parts into strings and then combines them. It places the optional after_dollars and after_cents strings after the dollars and cents strings. For example, by default on input 12.34 the function returns "twelve DOLLARS AND thirty four CENTS."
|
|
' Return a word representation of a dollar amount.
Private Function DollarsToString(ByVal money As Double, _
Optional ByVal after_dollars As String = " DOLLARS AND " & _
"", Optional ByVal after_cents As String = " CENTS") As _
String
' Get the dollar and cents parts.
Dim dollars As Double = Int(money)
Dim cents As Double = CInt(100 * (money - dollars))
' Convert the parts into words.
Dim dollars_str As String = NumberToString(dollars)
Dim cents_str As String = NumberToString(cents)
Return _
dollars_str & after_dollars & _
cents_str & after_cents
End Function
|
|
To convert a dollar value into a string, the program simply calls function DollarsToString and displays the result. The following code shows two functions calls (one commented out) showing two different output styles. You can modify the code to produce such effects as proper case numbers as in "Twelve dollars and Thirty Four cents."
|
|
'txtResult.Text = DollarsToString(CDbl(txtNumber.Text), "
' AND ", "/100 DOLLARS")
txtResult.Text = DollarsToString(CDbl(txtNumber.Text))
|
|
|
|
|
|