CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    9

    Numbers too Text Value

    Is it possible too change numbers into text?. I have two textboxes, textbox one has a value like 85.22, can textbox2 read as Eighty Five Dollars and 22/100 ?. No matter what textbox1 value is it will read text in the second textbox.
    Thanks for any Help you can give me. Z

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Numbers too Text Value

    Sure. Just create a few IF statements, to add from the BIGGEST value to the SMALLEST, and you should be fine. If you get stuck, post the code...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2010
    Posts
    9

    Re: Numbers too Text Value

    Thank You, I got this code off the interent and I'm not sure how too adapt for my needs. New too VS so I'm a little slow on this. This code does the words nice, I need textbox 2 value too control the words. Not sure how too get 22/100 after the text. For example I'm using 85.22 in text box 2.
    Thanks
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Numbers too Text Value

    You can subtract the INT() value of 85.22, to get 85, then subtract that from 85.22. That will give you 22

    If there is any CentVal, you'd use this: (freehand)
    Code:
    string.format("{0} Dollars and {1} / 100", DolVal, CentVal)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Dec 2010
    Posts
    9

    Re: Numbers too Text Value

    Just not sure what too do, want textbox1 to read value of textbox2 in words.getting error at
    tempstr ?
    Public Class Form1

    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
    TextBox1.Text = (TextBox2.Text)

    End Sub
    Public Function numtoword(ByVal numstr As Object) As String
    ' The best data type to feed in is
    ' Decimal, but it is up to you

    Dim tempstr As String
    Dim newstr As String
    numstr = CDec(numstr)

    If numstr = 0 Then
    numtoword = "zero "
    Exit Function
    End If

    If numstr > 10 ^ 24 Then
    numtoword = "Too big"
    Exit Function
    End If

    If numstr >= 10 ^ 12 Then
    newstr = numtoword(Int(numstr / 10 ^ 12))
    numstr = ((numstr / 10 ^ 12) - _
    Int(numstr / 10 ^ 12)) * 10 ^ 12
    If numstr = 0 Then
    tempstr = tempstr & newstr & "billion " (This is problemline)
    Else
    tempstr = tempstr & newstr & "billion, "
    End If
    End If

    If numstr >= 10 ^ 6 Then
    newstr = numtoword(Int(numstr / 10 ^ 6))
    numstr = ((numstr / 10 ^ 6) - _
    Int(numstr / 10 ^ 6)) * 10 ^ 6
    If numstr = 0 Then
    tempstr = tempstr & newstr & "million "
    Else
    tempstr = tempstr & newstr & "million, "
    End If
    End If

    If numstr >= 10 ^ 3 Then
    newstr = numtoword(Int(numstr / 10 ^ 3))
    numstr = ((numstr / 10 ^ 3) - _
    Int(numstr / 10 ^ 3)) * 10 ^ 3
    If numstr = 0 Then
    tempstr = tempstr & newstr & "thousand "
    Else
    tempstr = tempstr & newstr & "thousand, "
    End If
    End If

    If numstr >= 10 ^ 2 Then
    newstr = numtoword(Int(numstr / 10 ^ 2))
    numstr = ((numstr / 10 ^ 2) - _
    Int(numstr / 10 ^ 2)) * 10 ^ 2
    If numstr = 0 Then
    tempstr = tempstr & newstr & "hundred "
    Else
    tempstr = tempstr & newstr & "hundred and "
    End If
    End If

    If numstr >= 20 Then
    Select Case Int(numstr / 10)
    Case 2
    tempstr = tempstr & "twenty "
    Case 3
    tempstr = tempstr & "thirty "
    Case 4
    tempstr = tempstr & "forty "
    Case 5
    tempstr = tempstr & "fifty "
    Case 6
    tempstr = tempstr & "sixty "
    Case 7
    tempstr = tempstr & "seventy "
    Case 8
    tempstr = tempstr & "eighty "
    Case 9
    tempstr = tempstr & "ninety "
    End Select
    numstr = ((numstr / 10) - _
    Int(numstr / 10)) * 10
    End If

    If numstr > 0 Then
    Select Case numstr
    Case 1
    tempstr = tempstr & "one "
    Case 2
    tempstr = tempstr & "two "
    Case 3
    tempstr = tempstr & "three "
    Case 4
    tempstr = tempstr & "four "
    Case 5
    tempstr = tempstr & "five "
    Case 6
    tempstr = tempstr & "six "
    Case 7
    tempstr = tempstr & "seven "
    Case 8
    tempstr = tempstr & "eight "
    Case 9
    tempstr = tempstr & "nine "
    Case 10
    tempstr = tempstr & "ten "
    Case 11
    tempstr = tempstr & "eleven "
    Case 12
    tempstr = tempstr & "twelve "
    Case 13
    tempstr = tempstr & "thirteen "
    Case 14
    tempstr = tempstr & "fourteen "
    Case 15
    tempstr = tempstr & "fifteen "
    Case 16
    tempstr = tempstr & "sixteen "
    Case 17
    tempstr = tempstr & "seventeen "
    Case 18
    tempstr = tempstr & "eighteen "
    Case 19
    tempstr = tempstr & "nineteen "
    End Select
    numstr = ((numstr / 10) - Int(numstr / 10)) * 10
    End If
    numtoword = tempstr
    End Function



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub




    End Class

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Numbers too Text Value

    Looks like you're making it too complicated. Here's some VB6 code, so you can see the logic that it uses. Using Exponents is probably converting into Long, btw.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Dim num As Long
      Dim str As String
      num = Val(Text1.Text)
      str = convert(num)
      MsgBox str
    End Sub
    
    Private Sub Form_Load()
      Text1.Text = 1240
    End Sub
    
    Function convert(n As Long) As String
      Dim s As String
      Dim x As Integer
      If n > 1000 Then
        x = (n \ 1000)
        s = sngl(x) & " Thousand"
        n = n - (x * 1000)
      End If
      If n > 100 Then
        x = (n \ 100)
        s = s & sngl(x) & " Hundred "
        n = n - (x * 100)
      End If
      If n > 10 Then
        x = (n \ 10)
        Select Case x
        Case 2
          s = s & "Twenty"
        Case 3
          s = s & "Thirty"
        Case 4
          s = s & "Fourty"
        Case 5
          s = s & "Fifty"
        Case 6
          s = s & "Sixty"
        Case 7
          s = s & "Seventy"
        Case 8
          s = s & "Eighty"
        Case 9
          s = s & "Ninety"
        End Select
        n = n - (x * 10)
      End If
      s = s & sngl(CInt(n))
      If n = 0 Then s = "Zero"
      convert = s
    End Function
    
    Function sngl(x As Integer) As String
        Dim s As String
        Select Case x
        Case 1
          s = s & " One"
        Case 2
          s = s & " Two"
        Case 3
          s = s & " Three"
        Case 4
          s = s & " Four"
        Case 5
          s = s & " Five"
        Case 6
          s = s & " Six"
        Case 7
          s = s & " Seven"
        Case 8
          s = s & " Eight"
        Case 9
          s = s & " Nine"
        End Select
        sngl = s
    End Function
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured