|
|
Title | Convert really huge dollar values into words in Visual Basic 6 |
Description | This example shows how to convert really huge dollar values into words in Visual Basic 6. |
Keywords | convert number, convert dollars, dollar, dollar value, cents, number, words, hundred, thousand, million, billion, trillion, quadrillion, milliard |
Categories | Algorithms |
|
|
Function GroupToWords returns a string representing a number between 1 and 999. If the number is 0, it returns an empty string.
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 done_before As Boolean
Static one_to_nineteen() As String
Static multiples_of_ten() As String
Dim digit As Integer
Dim result As String
If Not done_before Then
done_before = True
one_to_nineteen = Split("zero,one,two,three," & _
"four,five,six,seven,eight,nine,ten," & _
"eleven,twelve,thirteen,fourteen,fifteen," & _
"sixteen,seventeen,eightteen,nineteen", ",")
multiples_of_ten = Split("twenty,thirty," & _
"forty,fifty,sixty,seventy,eighty,ninety", ",")
End If
' If the number is 0, return an empty string.
GroupToWords = ""
If num = 0 Then Exit Function
' Handle the hundreds digit.
result = ""
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
GroupToWords = Trim$(result)
Exit Function
End If
' See if the rest is less than 20.
If num < 20 Then
' Look up the correct name.
result = result & " " & one_to_nineteen(num)
Else
' Handle the tens digit.
digit = num \ 10
num = num Mod 10
result = result & " " & multiples_of_ten(digit - 2)
' Handle the final digit.
If num > 0 Then
result = result & " " & one_to_nineteen(num)
End If
End If
GroupToWords = Trim$(result)
End Function
|
|
Function NumberToString takes a number as a string input and returns a string representation of the number. It takes the input as a string to allow really huge numbers. The Decimal and Double data types cannot store truly enormous numbers without precision errors.
First the function checks its use_us_group_names parameter and initializes its groups array to a list of group names that is either appropriate for US/scientific use or for non-US use.
Next the function cleans up its input string a bit. It removes "$", "," and leading zeros. If the string contains a decimal point, it removes anything after it.
The code then determines how many groups of three digits the number needs. It pads the string on the left so its length is a multiple of 3.
Now the code steps through the number's three-digit groups. For each group, it extracts the digits, converts them into a number, uses function GroupToWords to convert the group into a string, and adds the group's name to the result.
Finally the function removes the trailing ", " if present and returns the result.
|
|
' Return a word representation of the whole number value.
Private Function NumberToString(ByVal num_str As String, _
Optional ByVal use_us_group_names As Boolean = True) As _
String
Const CURRENCY_CHAR As String = "$"
Const SEPARATOR As String = ","
Const DECIMAL_POINT As String = "."
Dim groups() As String
Dim pos As Integer
Dim num_groups As Integer
Dim result As String
Dim group_num As Integer
Dim group_str As String
Dim group_value As Integer
' Get the appropiate group names.
If use_us_group_names Then
groups = Split(",thousand,million,billion," & _
"trillion,quadrillion,quintillion," & _
"sextillion,septillion,octillion," & _
"nonillion,decillion,undecillion," & _
"duodecillion,tredecillion," & _
"quattuordecillion,quindecillion," & _
"sexdecillion,septendecillion," & _
"octodecillion,novemdecillion," & _
"vigintillion", ",")
Else
groups = Split(",thousand,million," & _
"milliard,billion,1000 billion," & _
"trillion,1000 trillion,quadrillion," & _
"1000 quadrillion,quintillion," & _
"1000 quintillion,sextillion," & _
"1000 sextillion,septillion," & _
"1000 septillion,octillion," & _
"1000 octillion,nonillion," & _
"1000 nonillion,decillion," & _
"1000 decillion", ",")
End If
' Clean the string a bit.
' Remove "$", ",", leading zeros, and
' anything after a decimal point.
num_str = Replace$(num_str, CURRENCY_CHAR, "")
num_str = Replace$(num_str, SEPARATOR, "")
Do While Left$(num_str, 1) = "0"
num_str = Mid$(num_str, 2)
Loop
pos = InStr(num_str, DECIMAL_POINT)
If pos = 1 Then
NumberToString = "zero"
Exit Function
ElseIf pos > 1 Then
num_str = Left$(num_str, pos - 1)
End If
' See how many groups there will be.
num_groups = (Len(num_str) + 2) \ 3
' Pad so length is a multiple of 3.
num_str = Space$(num_groups * 3 - Len(num_str)) & _
num_str
' Process the groups, largest first.
result = ""
For group_num = num_groups - 1 To 0 Step -1
' Get the next three digits.
group_str = Mid$(num_str, 1, 3)
num_str = Mid$(num_str, 4)
group_value = CInt(group_str)
' Convert the group into words.
If group_value > 0 Then
If group_num >= UBound(groups) Then
result = result & GroupToWords(group_value) _
& _
" ?, "
Else
result = result & GroupToWords(group_value) _
& _
" " & groups(group_num) & ", "
End If
End If
Next group_num
' Remove the trailing ", ".
If Right$(result, 2) = ", " Then
result = Left$(result, Len(result) - 2)
ElseIf Len(result) = 0 Then
result = "zero"
End If
NumberToString = Trim$(result)
End Function
|
|
The DollarsToString function takes a dollar value as a string input. It cleans the string by removing "$" and "," characters. It breaks the string into its dollar and cents parts and uses NumberToString to convert them into words. It combines the two strings and returns the result.
|
|
' Return a word representation of a dollar amount.
Private Function DollarsToString(ByVal num_str As String, _
Optional ByVal after_dollars As String = " DOLLARS AND " & _
"", Optional ByVal after_cents As String = " CENTS", _
Optional ByVal use_us_group_names As Boolean = True) As _
String
Const CURRENCY_CHAR As String = "$"
Const SEPARATOR As String = ","
Const DECIMAL_POINT As String = "."
Dim pos As Integer
Dim dollars As String
Dim cents As String
Dim dollars_str As String
Dim cents_str As String
' Clean the string a bit.
' Remove "$", ",", and leading zeros.
num_str = Replace$(num_str, CURRENCY_CHAR, "")
num_str = Replace$(num_str, SEPARATOR, "")
Do While Left$(num_str, 1) = "0"
num_str = Mid$(num_str, 2)
Loop
' Get the dollar and cents parts.
pos = InStr(num_str, DECIMAL_POINT)
dollars = ""
cents = ""
If pos < 1 Then
dollars = num_str
ElseIf pos = 1 Then
cents = Mid$(num_str, pos + 1)
Else
dollars = Left$(num_str, pos - 1)
cents = Mid$(num_str, pos + 1)
End If
' Convert the cents string into 1/100ths of a dollar.
cents = cents & "0"
cents = Left$(cents, 2) & "." & Mid$(cents, 3)
cents = Format$(CInt(cents))
' Convert the parts into words.
dollars_str = NumberToString(dollars, _
use_us_group_names)
cents_str = NumberToString(cents, use_us_group_names)
DollarsToString = _
dollars_str & after_dollars & _
cents_str & after_cents
End Function
|
|
To convert a number into a string, the program simply calls function DollarsToString and displays the result.
|
|
txtResult.Text = _
DollarsToString( _
txtNumber.Text, _
, , chkUseUsGroupNames.Value = vbChecked)
|
|
|
|
|
|