Converting Numbers to Strings with Currencies
There have been a few articles on this topic but I haven’t found any that took into consideration currencies other than US Dollars and with different smaller denominations (for example, there are 1000 fils to the Kuwaity Dinar but only 100 cents to the US Dollar).
The most recent post I saw about Converting Numbers to Words was on pdbook.com (with a link to the Microsoft KnowledgeBase Article).
So here’s my version of this UDF. It’s based on the code from pdbook.com but is recursive and accepts parameters to define (a) the name of the currency, (b) the name of the smaller denomination and (c) the decimal precision.
Function SpellNumber(ByVal strAmount As String, strCur As String, strDec As String, iPrec As Integer)
Dim BigDenom As String, SmallDenom As String, Temp As String
Dim iDecimalPlace As Integer
Dim Count As Integer
ReDim Place(9) As String
Place(2) = ” Thousand “
Place(3) = ” Million “
Place(4) = ” Billion “
Place(5) = ” Trillion “
‘ String representation of amount.
strAmount = Trim(Str(strAmount))
‘ Position of decimal place 0 if none.
iDecimalPlace = InStr(strAmount, “.”)
‘ Separate the Integer part from the decimals.
If iDecimalPlace > 0 Then
SmallDenom = Left(Right(strAmount, Len(strAmount) - iDecimalPlace) & “0000000000″, iPrec)
SmallDenom = SpellNumber(SmallDenom, strDec, “”, 0)
BigDenom = Left(strAmount, iDecimalPlace - 1)
BigDenom = SpellNumber(BigDenom, strCur, “”, 0)
SpellNumber = BigDenom & ” and ” & SmallDenom
Exit Function
End If
If iDecimalPlace = 0 Then
Count = 1
Do While strAmount <> “”
Temp = GetHundreds(Right(strAmount, 3))
If Temp <> “” Then BigDenom = Temp & Place(Count) & BigDenom
If Len(strAmount) > 3 Then
strAmount = Left(strAmount, Len(strAmount) - 3)
Else
strAmount = “”
End If
Count = Count + 1
Loop
Select Case BigDenom
Case “”
BigDenom = “No ” & strCur
Case “One”
BigDenom = “One ” & strCur
Case Else
BigDenom = BigDenom & ” ” & strCur
End Select
SpellNumber = BigDenom
End If
End Function
‘ Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right(”000″ & MyNumber, 3)
‘ Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> “0″ Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & ” Hundred “
End If
‘ Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> “0″ Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
‘ Converts a number from 10 to 99 into text.
Function GetTens(TensText)
Dim Result As String
Result = “” ‘ Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ‘ If value between 10-19…
Select Case Val(TensText)
Case 10: Result = “Ten”
Case 11: Result = “Eleven”
Case 12: Result = “Twelve”
Case 13: Result = “Thirteen”
Case 14: Result = “Fourteen”
Case 15: Result = “Fifteen”
Case 16: Result = “Sixteen”
Case 17: Result = “Seventeen”
Case 18: Result = “Eighteen”
Case 19: Result = “Nineteen”
Case Else
End Select
Else ‘ If value between 20-99…
Select Case Val(Left(TensText, 1))
Case 2: Result = “Twenty “
Case 3: Result = “Thirty “
Case 4: Result = “Forty “
Case 5: Result = “Fifty “
Case 6: Result = “Sixty “
Case 7: Result = “Seventy “
Case 8: Result = “Eighty “
Case 9: Result = “Ninety “
Case Else
End Select
Result = Result & GetDigit _
(Right(TensText, 1)) ‘ Retrieve ones place.
End If
GetTens = Result
End Function
‘ Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = “One”
Case 2: GetDigit = “Two”
Case 3: GetDigit = “Three”
Case 4: GetDigit = “Four”
Case 5: GetDigit = “Five”
Case 6: GetDigit = “Six”
Case 7: GetDigit = “Seven”
Case 8: GetDigit = “Eight”
Case 9: GetDigit = “Nine”
Case Else: GetDigit = “”
End Select
End Function
The formula : “=spellnumber(”1234.5678″,”Dinar”,”Fils”,3)” would therefore result in “One Thousand Two Hundred Thirty Four Dinar and Five Hundred Sixty Seven Fils”
<UPDATE> Bob Phillips at XLDynamic also has a different version here.