|
- 'Val函数取值为从第一个字符取值,如果是数字就一直取到能识别的数字为止,第一个不是数字后面就停止识别,默认为取0值。
- Function Val(byval str)
- Dim arr,validStr
- Dim items,num
- Dim seenDot,seenDigit
- num=0
- Val = 0
- validStr=""
- seenDot = false
- seenDigit = false
-
- for i = 1 to len(str)
- items=mid(str,i,1)
-
- if items="." and seenDot=false then
- seenDot = true
- validStr=validStr&items
-
- elseif (items="-" or items="+") and seenDigit=false then
- validStr=validStr&items
-
- elseif IsNumeric(items)=true then
- seenDigit = true
- validStr=validStr&items
- elseif items=" " or isnull(items) then
-
- else
- exit for
- end if
-
- next
-
- if validStr<>"" then val=CDbl(validStr) end if
- End Function
- print val(" 2 3 . 5 8 . 54")
- 输出结果:23.58
复制代码
|
|