Restored from previous forum. Originally posted by Cantor.
Hi!
I Want the Val-Function to return a correct value even if there are no numbers in the string.
Val("12.34") should result 12.34
Val("12ab34") should result 1234.
Val("PureBasic") should result 0.
The Val-Fuction of other Basic-Dialects work correctly.
Is there a possibility to correct this in PB?
--
Best regards,
Martin
[Implemented] Val Function
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by wayne1.
;maybe this helps
;download charactertest library from http://www.reelmediaproductions.com/pb
Procedure.l ValEx(string$)
count=1
While count <= Len(string$)
If IsDigit(Mid(string$, count, 1)); Or "."= Mid(string$, count, 1) ;if pb handled floats
temp$=temp$+Mid(string$, count, 1)
EndIf
count=count+1
Wend
ret_val=Val(temp$)
ProcedureReturn ret_val
EndProcedure
a=ValEx("12.34")
MessageRequester("12.34 PB can't handle floats yet",Str(a) ,0)
a=ValEx("12ab34")
MessageRequester("12ab34",Str(a) ,0)
a=ValEx("PureBasic")
MessageRequester("PureBasic",Str(a) ,0)
Edited by - wayne1 on 07 November 2001 06:57:03
;maybe this helps
;download charactertest library from http://www.reelmediaproductions.com/pb
Procedure.l ValEx(string$)
count=1
While count <= Len(string$)
If IsDigit(Mid(string$, count, 1)); Or "."= Mid(string$, count, 1) ;if pb handled floats
temp$=temp$+Mid(string$, count, 1)
EndIf
count=count+1
Wend
ret_val=Val(temp$)
ProcedureReturn ret_val
EndProcedure
a=ValEx("12.34")
MessageRequester("12.34 PB can't handle floats yet",Str(a) ,0)
a=ValEx("12ab34")
MessageRequester("12ab34",Str(a) ,0)
a=ValEx("PureBasic")
MessageRequester("PureBasic",Str(a) ,0)
Edited by - wayne1 on 07 November 2001 06:57:03
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by wayne1.
;correct method
;download charactertest library from http://www.reelmediaproductions.com/pb
;calldebugger
Procedure.l ValEx(string$)
count=1
While count <= Len(string$)
If IsDigit(Mid(string$, count, 1)); Or "."= Mid(string$, count, 1) ;if pb handled floats
temp$=temp$+Mid(string$, count, 1)
Else
count=Len(string$)
EndIf
count=count+1
Wend
ret_val=Val(temp$)
ProcedureReturn ret_val
EndProcedure
a=ValEx("12.34")
MessageRequester("12.34 PB can't handle floats yet",Str(a) ,0)
a=ValEx("12ab34")
MessageRequester("12ab34",Str(a) ,0)
a=ValEx("PureBasic")
MessageRequester("PureBasic",Str(a) ,0)
Edited by - wayne1 on 13 December 2001 05:00:26
;correct method
;download charactertest library from http://www.reelmediaproductions.com/pb
;calldebugger
Procedure.l ValEx(string$)
count=1
While count <= Len(string$)
If IsDigit(Mid(string$, count, 1)); Or "."= Mid(string$, count, 1) ;if pb handled floats
temp$=temp$+Mid(string$, count, 1)
Else
count=Len(string$)
EndIf
count=count+1
Wend
ret_val=Val(temp$)
ProcedureReturn ret_val
EndProcedure
a=ValEx("12.34")
MessageRequester("12.34 PB can't handle floats yet",Str(a) ,0)
a=ValEx("12ab34")
MessageRequester("12ab34",Str(a) ,0)
a=ValEx("PureBasic")
MessageRequester("PureBasic",Str(a) ,0)
Edited by - wayne1 on 13 December 2001 05:00:26