Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSCOMM

Status
Not open for further replies.

wlaflamme

Technical User
Aug 1, 2003
22
US
I am new to VB and i am having a difficult time with converting a string to a double. The reason i need to do this is because I am reading and writing to a motion controller via my RS 232 port. The data i recieve in has some characters and numbers which i need to extract The following is an example of the data string:

BufferIn = &quot;3.234<&quot;

I need to set the Double Variable to equal 3.234 and drop the extra characters. Any Ideas about how to do this?
 
Use the Val() or the Cdbl() function ?

BufferIn = &quot;3.234<&quot;
Val(BufferIn) will give you 3.234 as whatever numeric type is required

Cdbl(BufferIn) will give 3.234 as a Double

See VBHelp for details

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Could do something like:

Function fTextToDouble(Byval strIn As String) As Double

'--- Given a string, removes non numeric chars and
' returns the remaining chars as a double

Dim strTmp As String
Dim strOut As String
Dim intC1 As Integer

If strIn = vbNullString Then
Exit Function
End If

'Move through the string and check each char
For intC1 = 1 To Len(strIn)
strTmp = Mid$(strIn, intC1, 1)
If IsNumeric(strTmp) Or strTmp = &quot;.&quot; _
Or strTmp = &quot;-&quot; Then
'Add the char to the output buffer
strOut = strOut & strTmp
End If
Next

'Return value
fTextToDouble = Cdbl(Val(strOut))

End Function

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top