Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to convert vb .text to number format

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 02-07-2009
Member
 
Join Date: May 2008
Posts: 222
How to convert vb .text to number format
  

I have the requirement to convert the word 2007 text file and need to have visual basic code for converting a selected text to a number, and a number to text? If it would be possible then please let me, i also expect answer descriptive as well.

Thanks for those who help..

Reply With Quote
  #2  
Old 02-07-2009
Member
 
Join Date: Dec 2008
Posts: 183
Re: How to convert vb .text to number format

The number to be converted to word text can be a numeric value or a string.
  • If the optional formatting keyword is omitted, a number such as 1234 will be returned as "One Thousand Two Hundred Thirty Four".
  • If the word "check" is passed, the number returned will be written as on a check. Hence, 123.45 will report back as "One Hundred Twenty Three and 45/100". If there are more than two decimal places in the "check" mode, the decimal portion will be rounded to the nearest two decimal places.
Reply With Quote
  #3  
Old 02-07-2009
Member
 
Join Date: Jan 2006
Posts: 211
Re: How to convert vb .text to number format

To convert String to Number i will suggest you to use the Val function which will convert a string to a decimal number.If you need a formula to simply change numbers to text, use =concatenate([Cell#]). The Val function is not locale aware, so it will not recognize comma decimal separators in countries where this is used, such as "1,000,000".

following is the vb code which will allows you to do the conversion.
Quote:
Dim v As Integer

v = Val("100 Blah Blah")

Print v ' prints 100

v = Val("Nothing")

Print v ' Could not convert, prints 0
Reply With Quote
  #4  
Old 02-07-2009
Member
 
Join Date: Oct 2008
Posts: 167
Re: How to convert vb .text to number format

Code:
found = InStrRev(Text1.Text, Chr(10), pos, 0)
Text1.text is the string being searched and chr(10) is the string being searched. We take a new variable called pos, which signifies the starting postion for the search in the given string (i.e. text1.text). The SendMessage API function requires the window handle (hWnd& above) of the text box. To get the window handle of the text box, use the hWnd property of the text box. Initially pos is set as the current caret location, which is given by text1.selstart Pos is taken since when we find a new line char, its obvious that we will look for a new line char above of it.
Reply With Quote
  #5  
Old 04-07-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: How to convert vb .text to number format

' Convert a number between 0 and 999 into words.
Private Function numbers(ByVal num As Integer) As String
Static onetonineteen() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen"}
Static tens() As String = {"Twenty -", "Thirty -", "Forty -", "Fifty -", "Sixty -", "Seventy -", "Eighty -", "Ninety -"}
Static thousand() As String = {" Thousand "}
Dim place As Integer
Dim valid As Boolean = True
Dim result As String = ""
'http://www.vb-helper.com/howto_net_dollars_to_words.html is somewhat used here



' Handle the hundreds digit.
If txtAmount.Text.Length = 0 Then
MessageBox.Show("Please enter an amount.")
valid = False
End If
If num > 99 Then
place = CInt(num / 100)
num = num Mod 100
result = onetonineteen(place) & " Hundred"
End If

' If num = 0, we have hundreds only.
If num = 0 Then Return result.Trim()

' See if the rest is less than 20.
If num < 20 Then
' Look up the correct name.
result &= " " & onetonineteen(num)
Else
' Handle the tens digit.
place = num \ 10
num = num Mod 10
result &= " " & tens(place - 2)

' Handle the final digit.
If num > 0 Then
result &= " " & onetonineteen(num)
valid = True
End If

If result.EndsWith(", ") Then
result = result.Substring(0, result.Length - 2)
End If
If valid = True Then
Me.Hide()
End If
If valid = False Then
txtAmount.Text = ""
End If

End If

Return result.Trim()
End Function
Reply With Quote
  #6  
Old 04-07-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: How to convert vb .text to number format

Private Function NumberToString(ByVal num As Double) As _
String

' Remove any fractional part.
num = Int(num)

' If the number is 0, return zero.
If num = 0 Then Return "zero"

Static groups() As String = {"", "thousand", "million", _
"billion", "trillion", "quadrillion", "?", "??", _
"???", "????"}
Dim result As String = ""
Dim valid As Boolean = True
' Process the groups, smallest first.
Dim quotient As Double
Dim remainder As Integer
Dim group_num As Integer = 0
Do While num > 0 And num < 9999.99
' Get the next group of three digits.
quotient = Int(num / 1000)
remainder = CInt(num - quotient * 1000)
num = quotient

' Convert the group into words.
result = numbers(remainder) & _
" " & groups(group_num) & ", " & _
result

' Get ready for the next group.
group_num += 1
valid = True
Loop

If txtAmount.Text.Length = 0 Then
MessageBox.Show("Please enter an amount.")
valid = False
End If
If num < 0 And num > 9999.99 Then
MessageBox.Show("Amount must be greater then 0 and less then $10,000 .")
txtAmount.Text = String.Empty
valid = False
End If

If valid = True Then
Me.Hide()
End If




' Remove the trailing ", ".
If result.EndsWith(", ") Then
result = result.Substring(0, result.Length - 2)
End If

Return result.Trim()
End Function
Reply With Quote
  #7  
Old 04-07-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: How to convert vb .text to number format

View the above samples but please dont just out right copy it unless it was a share ware thingy.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert vb .text to number format"
Thread Thread Starter Forum Replies Last Post
How to convert number to text in excel Jevin Software Development 9 17-05-2012 06:42 PM
How to convert math text PDF format to Kindle readable format The#Gaelic Portable Devices 7 23-02-2012 11:09 AM
How to convert HTML to TEXT format ? Custidio Windows Software 5 09-02-2010 01:22 AM
Convert a text document in PDF format (Mac) Waffle Operating Systems 4 03-02-2010 09:33 PM
How can I convert text data into table format? Ram Bharose Windows Software 7 30-12-2009 01:58 PM


All times are GMT +5.5. The time now is 04:09 PM.