Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Help me to fix: in my RDLC file i have given a text box to generate the amount value in word. Its doing every thing correct but when i am generating amount like 15,000.00 its showing "Ten Thousand" or when i am giving amount as 115,000.00 its showing "One hundred Ten Thousand" & its goes on for 215,000.00 or 315,000.00

What I have tried:

VB
Function SpellNumber(ByVal MyNumber As Decimal, ByVal Currency As String) As String
    Dim Dollars As String
    Dim Cents As String
    Dim Temp As String
    Dim DecimalPlace As Integer
    Dim Count As Integer
    Dim Place(5) As String

    Place(2) = " Thousand "
    Place(3) = " Million "
    Place(4) = " Billion "
    Place(5) = " Trillion "

    Dim MyNumberStr As String = CStr(MyNumber)

    DecimalPlace = InStr(MyNumberStr, ".")

    If DecimalPlace > 0 Then
        Cents = " & " & Mid(MyNumberStr, DecimalPlace + 1, 2) & "/100"
        MyNumberStr = Left(MyNumberStr, DecimalPlace - 1)
    Else
        Cents = "& XX/100"
    End If

    Count = 1
    Do While MyNumberStr <> ""
        Temp = GetHundreds(Right(MyNumberStr, 3))
        If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
        If Len(MyNumberStr) > 3 Then
            MyNumberStr = Left(MyNumberStr, Len(MyNumberStr) - 3)
        Else
            MyNumberStr = ""
        End If
        Count = Count + 1
    Loop

    Select Case Dollars
        Case ""
            Dollars = " "
        Case "One"
            Dollars = " "
        Case Else
            Dollars = StrConv(Dollars, VbStrConv.ProperCase) & " "
    End Select

    ' Concatenate the Currency field value with the spelled-out amount
    SpellNumber = Currency & " " & Dollars & Cents
End Function

Function GetHundreds(ByVal MyNumber) As String
    Dim Result As String

    If Val(MyNumber) = 0 Then Exit Function

    MyNumber = Right("000" & MyNumber, 3)

    If Mid(MyNumber, 1, 1) <> "0" Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
    End If

    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

Function GetTens(TensText) As String
    Dim Result As String

    Result = ""

    If Val(Left(TensText, 1)) = 1 Then
        Select Case Val(TensText)
            Case 10 To 19
                Result = "Ten"
            Case Else
        End Select
    Else
        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))
    End If

    GetTens = Result
End Function

Function GetDigit(Digit) As String
    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
Posted
Updated 22-Jan-24 9:08am
v2
Comments
[no name] 18-Jan-24 13:55pm    
I think you need a routine that is easier to understand and maintain:

https://stackoverflow.com/questions/22060540/display-amount-in-words-in-vb-net

1 solution

Have a look at your GetTens function. You currently have
VB
Select Case Val(TensText)
      Case 10 To 19
            Result = "Ten"
A starting point would be to actually enumerate all the teens ...
VB
Select Case Val(TensText)
      Case 10
            Result = "Ten"
      Case 11
            Result = "Eleven"
      Case 12
            Result = "Twelve"
      etc...
Your next problem is getting "15 Thousand" instead of "150 Thousand". Hint - have a look at your GetHundreds function and its call ... 100 only has 2 zeroes - you're handling 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900