[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I spell out a number in VB.NET/ASP.NET 1.1?

Posted on 2009-05-06
3
Medium Priority
?
1,219 Views
Last Modified: 2013-11-07
I have an integer that will always be a whole number between 1 and 20, but I'd like to display it for my users in it's spelled-out form. "1" would be "one", "2" would be "two", and so on up to 20. How would I go about accomplishing this in ASP.NET 1.1? Seems so simple but I could not find it on the Google.
0
Comment
Question by:CitySec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 24314859
This isn't VB.net but it should work with minimal changes.

The routine below, GetSpelledNumber, converts a real number into a spelled out number (English only). See the comments in the routine for instructions on how to use.

Examples:

GetSpelledNumber(1234) = One Thousand Two Hundred Thirty Four
GetSpelledNumber(1234.56) = One Thousand Two Hundred Thirty Four Point Five Six
GetSpelledNumber(1234.56, TRUE) = One Thousand Two Hundred Thirty Four Dollars and Fifty Six Cents

[Begin Code Segment]

Public Function GetSpelledNumber( _
      ByVal Number As Double, _
      Optional ByVal ReturnCurrency As Boolean, _
      Optional ByVal ReturnFraction As Boolean _
   ) As String

' Return the number as a text string with each digit spelled out.
'
' Syntax
'
' GetSpelledNumber(Number, (ReturnCurrency])
'
' Number - Any double number.
'
' ReturnCurrency - Pass True to return the numnber as dollars and cents.
'   Optional. If omitted then False is assumed.
'
' ReturnFraction - Pass True to return any decimal number as a numeric
'   fraction versus words. When using this option, if there is no decimal
'   portion then the word "exactly" is appended to the result. Optional. If
'   omitted then False is assumed. Ignored if ReturnCurrency is True.
   
   Dim NumberParts As Variant
   Dim NumberText As String
   Dim Index As Long
   Dim Part As String
   Dim Result As String
   
   If Number = 0 Then
      If ReturnCurrency Then
         GetSpelledNumber = "No Dollars and No Cents"
      Else
         GetSpelledNumber = "Zero"
      End If
      Exit Function
   End If
   
   NumberParts = Split(Number, ".")
   NumberText = NumberParts(0)
   NumberText = Right("   " & NumberText, Int((Len(NumberText) + 2) / 3) * 3)
   For Index = 1 To Len(NumberText) / 3
      Part = GetSpelledNumberPart(CLng(Mid(NumberText, Len(NumberText) - 2 - (Index - 1) * 3, 3)))
      Select Case Index
         Case 1: Result = Part
         Case 2: Result = Part & " Thousand " & Result
         Case 3: Result = Part & " Million " & Result
         Case 4: Result = Part & " Billion " & Result
         Case 5: Result = Part & " Trillion " & Result
      End Select
   Next Index
   If ReturnCurrency Then Result = Result & " Dollar" & IIf(Not Result = "One", "s", vbNullString)
   If ReturnCurrency Then
      Result = Result & " and "
      If UBound(NumberParts) > 0 Then
         Part = GetSpelledNumberPart(Round(("0." & NumberParts(1)), 2) * 100)
      Else
         Part = GetSpelledNumberPart(0)
      End If
      If Len(Part) = 0 Then
         Result = Result & "No Cents"
      Else
         Result = Result & Part & " Cent" & IIf(Not Part = "One", "s", vbNullString)
      End If
   Else
      If UBound(NumberParts) > 0 Then
         If ReturnFraction Then
            Result = Result & " and " & CLng(NumberParts(1)) & "/1" & String(Len(NumberParts(1)), "0")
         Else
            Result = Result & " Point"
            For Index = 1 To Len(NumberParts(1))
               Result = Result & " " & GetSpelledNumberPart(Mid(NumberParts(1), Index, 1))
            Next Index
         End If
      Else
         If ReturnFraction Then Result = Result & " Exactly"
      End If
   End If
   
   GetSpelledNumber = Result

End Function

Private Function GetSpelledNumberPart( _
      ByVal NumberPart As String _
   ) As String

' Return the spelled out one to three digit number. Used by GetSpelledNumber.
'
' Syntax
'
' GetSpelledNumberPart(NumberPart)
'
' NumberPart - A one to three character string containing only numeric digits.
   
   Dim Index As Long
   Dim Result As String
   Dim Part As String
   
   NumberPart = Mid(NumberPart, 1, 3)
   For Index = 1 To Len(NumberPart)
      Part = vbNullString
      Select Case Index
         Case 1, 3
            Select Case Mid(NumberPart, Len(NumberPart) - Index + 1, 1)
               Case 1: Part = "One"
               Case 2: Part = "Two"
               Case 3: Part = "Three"
               Case 4: Part = "Four"
               Case 5: Part = "Five"
               Case 6: Part = "Six"
               Case 7: Part = "Seven"
               Case 8: Part = "Eight"
               Case 9: Part = "Nine"
            End Select
            If Index = 3 Then Part = Part & " Hundred"
         Case 2
            Select Case Mid(NumberPart, Len(NumberPart) - Index + 1, 1)
               Case 1
                  Result = vbNullString
                  Select Case Mid(NumberPart, Len(NumberPart), 1)
                     Case 0: Part = "Ten"
                     Case 1: Part = "Eleven"
                     Case 2: Part = "Twelve"
                     Case 3: Part = "Thirteen"
                     Case 4: Part = "Fourteen"
                     Case 5: Part = "Fifteen"
                     Case 6: Part = "Sixteen"
                     Case 7: Part = "Seventeen"
                     Case 8: Part = "Eighteen"
                     Case 9: Part = "Nineteen"
                  End Select
               Case 2: Part = "Twenty"
               Case 3: Part = "Thirty"
               Case 4: Part = "Forty"
               Case 5: Part = "Fifty"
               Case 6: Part = "Sixty"
               Case 7: Part = "Seventy"
               Case 8: Part = "Eighty"
               Case 9: Part = "Ninety"
            End Select
      End Select
      If Len(Part) > 0 Then Result = Part & IIf(Len(Result) = 0, vbNullString, " ") & Result
   Next Index
   
   GetSpelledNumberPart = Result

End Function

[End Code Segment]

Kevin
0
 
LVL 4

Expert Comment

by:cauos
ID: 24315070
i think you just need to convert number to string , if so this simple example to do this
 Dim arra(,) As String = {{"0", "zero"}, {"1", "one"}, {"2", "two"}, {"3", "three"}}
        Dim sentence As String = "112213"
        Dim sentenceToView As String = ""
        For i As Integer = 0 To sentence.Length - 1
            sentenceToView = sentenceToView & arra(Convert.ToUInt32(sentence(i).ToString()), 1) & ","
        Next

Open in new window

0
 
LVL 46

Accepted Solution

by:
tbsgadi earned 2000 total points
ID: 24315505
Convert Numbers to Words with VB.NET
http://www.freevbcode.com/ShowCode.asp?ID=8704

Gary
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question