This document contains code for a function that takes a numeric value as input and returns a string representing the number in words. It first checks that the number is valid, then breaks it into parts and converts each part into words by using an array of number words. It handles numbers from 0 to quadrillions, with commas for thousands separators, and returns a proper string like "One million, two hundred thirty-four thousand, five hundred sixty-one and 82/100 Only".
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views4 pages
Numberastext Jerome
This document contains code for a function that takes a numeric value as input and returns a string representing the number in words. It first checks that the number is valid, then breaks it into parts and converts each part into words by using an array of number words. It handles numbers from 0 to quadrillions, with commas for thousands separators, and returns a proper string like "One million, two hundred thirty-four thousand, five hundred sixty-one and 82/100 Only".
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
Private sNumberText() As String
Public Function NumberAsText(NumberIn As Variant) As String
Dim Cnt As Long Dim DecimalPoint As Long Dim CardinalNumber As Long Dim CommaAdjuster As Long Dim TestValue As Long Dim CurrValue As Currency Dim CentsString As String Dim NumberSign As String Dim WholePart As String Dim BigWholePart As String Dim DecimalPart As String Dim Tmp As String ' Dim bUseCheck As Boolean '---------------------------------------- ' Begin setting conditions for formatting '---------------------------------------- ' bUseCheck = True '---------------------------------------- ' Check/create array. If this is the first ' time using this routine, create the text ' strings that will be used. '---------------------------------------- If Not IsBounded(sNumberText) Then Call BuildArray(sNumberText) End If '---------------------------------------- ' Begin validating the number, and breaking ' into constituent parts '---------------------------------------- NumberIn = Trim$(NumberIn) If Not IsNumeric(NumberIn) Then NumberAsText = "Error - Number improperly formed" Exit Function Else DecimalPoint = InStr(NumberIn, ".") If DecimalPoint > 0 Then DecimalPart = Mid$(NumberIn, DecimalPoint + 1) WholePart = Left$(NumberIn, DecimalPoint - 1) Else DecimalPoint = Len(NumberIn) + 1 WholePart = NumberIn End If If InStr(NumberIn, ",,") Or _ InStr(NumberIn, ",.") Or _ InStr(NumberIn, ".,") Or _ InStr(DecimalPart, ",") Then NumberAsText = "Error - Improper use of commas" Exit Function ElseIf InStr(NumberIn, ",") Then CommaAdjuster = 0 WholePart = "" For Cnt = DecimalPoint - 1 To 1 Step -1 If Not Mid$(NumberIn, Cnt, 1) Like "[,]" Then WholePart = Mid$(NumberIn, Cnt, 1) & WholePart Else CommaAdjuster = CommaAdjuster + 1 If (DecimalPoint - Cnt - CommaAdjuster) Mod 3 Then NumberAsText = "Error - Improper use of commas" Exit Function End If End If Next End If End If If Left$(WholePart, 1) Like "[+-]" Then NumberSign = IIf(Left$(WholePart, 1) = "-", "Minus ", "Plus ") WholePart = Mid$(WholePart, 2) End If '---------------------------------------- ' Begin code to assure decimal portion of ' check value is not inadvertently rounded '---------------------------------------- CurrValue = CCur(Val("." & DecimalPart)) DecimalPart = Mid$(Format$(CurrValue, "0.00"), 3, 2) If CurrValue >= 0.995 Then If WholePart = String$(Len(WholePart), "9") Then WholePart = "1" & String$(Len(WholePart), "0") Else For Cnt = Len(WholePart) To 1 Step -1 If Mid$(WholePart, Cnt, 1) = "9" Then Mid$(WholePart, Cnt, 1) = "0" Else Mid$(WholePart, Cnt, 1) = _ CStr(Val(Mid$(WholePart, Cnt, 1)) + 1) Exit For End If Next End If End If '---------------------------------------- ' Final prep step - this assures number ' within range of formatting code below '---------------------------------------- If Len(WholePart) > 9 Then BigWholePart = Left$(WholePart, Len(WholePart) - 9) WholePart = Right$(WholePart, 9) End If If Len(BigWholePart) > 9 Then NumberAsText = "Error - Number too large" Exit Function ElseIf Not WholePart Like String$(Len(WholePart), "#") Or _ (Not BigWholePart Like String$(Len(BigWholePart), "#") _ And Len(BigWholePart) > 0) Then NumberAsText = "Error - Number improperly formed" Exit Function End If '---------------------------------------- ' Begin creating the output string '---------------------------------------- ' Very Large values TestValue = Val(BigWholePart) If TestValue > 999999 Then CardinalNumber = TestValue \ 1000000 Tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion " TestValue = TestValue - (CardinalNumber * 1000000) End If If TestValue > 999 Then CardinalNumber = TestValue \ 1000 Tmp = Tmp & HundredsTensUnits(CardinalNumber) & "Trillion " TestValue = TestValue - (CardinalNumber * 1000) End If If TestValue > 0 Then Tmp = Tmp & HundredsTensUnits(TestValue) & "Billion " End If ' Lesser values TestValue = Val(WholePart) If TestValue = 0 And BigWholePart = "" Then Tmp = "Zero " If TestValue > 999999 Then CardinalNumber = TestValue \ 1000000 Tmp = Tmp & HundredsTensUnits(CardinalNumber) & "Million " TestValue = TestValue - (CardinalNumber * 1000000) End If If TestValue > 999 Then CardinalNumber = TestValue \ 1000 Tmp = Tmp & HundredsTensUnits(CardinalNumber) & "Thousand " TestValue = TestValue - (CardinalNumber * 1000) End If If TestValue > 0 Then Tmp = Tmp & HundredsTensUnits(TestValue) End If ' Put fraction on end Tmp = Tmp & " and " & Left$(DecimalPart & "00", 2) Tmp = Tmp & "/100 Only" ' Done! NumberAsText = Replace(NumberSign & Tmp, "- ", " ") End Function
Private Function IsBounded(vntArray As Variant) As Boolean
On Error Resume Next IsBounded = IsNumeric(UBound(vntArray)) End Function
Private Function HundredsTensUnits(ByVal TestValue As Long) As String
Dim CardinalNumber As Long If TestValue > 99 Then CardinalNumber = TestValue \ 100 HundredsTensUnits = sNumberText(CardinalNumber) & " Hundred " TestValue = TestValue - (CardinalNumber * 100) End If If TestValue > 20 Then CardinalNumber = TestValue \ 10 HundredsTensUnits = HundredsTensUnits & _ sNumberText(CardinalNumber + 18) '& " " TestValue = TestValue - (CardinalNumber * 10) End If If TestValue > 0 Then HundredsTensUnits = HundredsTensUnits & _ sNumberText(TestValue) & " " End If If Right(HundredsTensUnits, 1) = "-" Then HundredsTensUnits = Left(HundredsTensUnits, Len(HundredsTensUnits) - 1) & " " End If End Function