Page 1 of 1
[Implemented] Val Function
Posted: Tue Nov 06, 2001 1:58 pm
by BackupUser
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
Posted: Wed Nov 07, 2001 6:50 am
by BackupUser
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
Posted: Wed Nov 07, 2001 6:01 pm
by BackupUser
Restored from previous forum. Originally posted by PB.
Val("12ab34") should result 1234.
Nope, the above should result 12, as it does in EVERY other basic version.
Posted: Thu Nov 08, 2001 5:01 am
by BackupUser
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