HI
What is the simplest way to find a number(like 0.72 or 6.73) in a text string using LotusScript?
Regards
HI
What is the simplest way to find a number(like 0.72 or 6.73) in a text string using LotusScript?
Regards
Subject: Use Val() or Cdbl() to convert string to double
val(“0.72”)
or
Cdbl("0.72)
are the two alternatives.
Cdbl() will respect the NotesInternational settings for decimal separator (i.e. in most of Europe, you would write “0,72”, whereas Val() always uses decimal point. As far as I recall, at least. The documentation does not provide any hints of this. Also, Val() is more “forgiving”, whereas Cdbl() will generate runtime errors when encountering a non-numeric string.
Subject: RE: Use Val() or Cdbl() to convert string to double
Val() is ok, but the string must lead with number of space e.g. " 13.562 text"
if you want to extract the number from a string like “BKK 10500 Thailand”
you may need to find the first number position and use mid$ or right$ to extract the string from that position to the end of string,
and then you can use Val()
Subject: RE: Use Val() or Cdbl() to convert string to double
As Nakorn pointed out, the string must begin with the number (except for leading space characters).
To extract a number from any position in a string try thius function:
Function extractDouble(Byval s As String) As Double
' returns the first embedded number in s, or 0, if no numbers exist.
' example: extract("hello 12.345 world") returns 12.345
Dim i As Integer
Dim result As Double
For i = 1 To Len(s)
If Instr("0123456789", Mid(s, i, 1)) > 0 Then
result = Val(Mid(s, i))
Exit For
End If
Next
extractDouble = result
End Function
You could also add a default return value, so this value is returned if no string exists (e.g. -1).
Function extractDouble(Byval s As String, byval default as double) As Double
' returns the first embedded number in s, or default, if no numbers exist.
' example: extractDouble("hello 12.345 world", -1) returns 12.345, whereas extractDouble("hello world", -1) returns -1.
Dim i As Integer
Dim result As Double
result = default
For i = 1 To Len(s)
If Instr("0123456789", Mid(s, i, 1)) > 0 Then
result = Val(Mid(s, i))
Exit For
End If
Next
extractDouble = result
End Function
Subject: Finding numbers in a text string
I don’t think there’s a really easy solution
You can use the Like function to look if there are numbers in the string. If so, you can use the instr function to locate the first number character. something like this :
text=“This is a test string 12.789 with a number in it”
pos=0
first=10000000000
for x= 0 to 9
pos = instr(text, trim(str(x)))
if (pos > 0) and (pos < first) then first = pos
next
now ‘first’ holds the position of the first ocuurence of numerical character. Now you’ll have to scan the characters right to position ‘first’ until you reach a non-numerical character, or the end of the string:
last=first
for x=first+1 to len(text)
if instr( mid(text,x,1) , “0.123456789” ) > 0 then
last=x
else
exit for
end if
number=val(mid(text,first, (last-first+1)))
if there could be more numbers in the string, you’ll have to truncate the string and start all over again :
text = right(text, len(text)-last)