VS:Str2Num: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Transfer Orso to _c_) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 33: | Line 33: | ||
[[User:CBM-c-|_c_]] (2017.11.18): Please be careful with overriding standard routines. I change the name of this routine into MyStr2Num. Thus it won't override the standard Str2Num. | |||
Author unknown, changed so that it doesn't override the standard routine: | |||
<code lang="pas"> | <code lang="pas"> | ||
FUNCTION | FUNCTION MyStr2Num(str :STRING) :REAL; | ||
{This | {This is more robust than Str2Num because it handles unit marks in the string, | ||
robust because it handles unit marks in the string, | |||
while the built-in FUNCTION does not. Returns zero | while the built-in FUNCTION does not. Returns zero | ||
if it fails, which of course does not unambiguously | if it fails, which of course does not unambiguously | ||
Line 48: | Line 48: | ||
num :REAL; | num :REAL; | ||
BEGIN | BEGIN | ||
MyStr2Num := 0; | |||
IF ValidNumStr(str, num) THEN | IF ValidNumStr(str, num) THEN MyStr2Num := num; | ||
END; | END; | ||
</code></remark> | </code></remark> |
Latest revision as of 07:50, 30 December 2020
.VectorScript|VectorScript ..VS:Function Reference|Function Reference ..VS:Function_Reference_Appendix|Appendix
Description
Function Str2Num returns the specified string as a numeric value.
FUNCTION Str2Num(
s :STRING) : REAL;
def vs.Str2Num(s): return REAL
Parameters
s STRING Source string.
Remarks
_c_ (2017.11.18): Please be careful with overriding standard routines. I change the name of this routine into MyStr2Num. Thus it won't override the standard Str2Num.
Author unknown, changed so that it doesn't override the standard routine:
FUNCTION MyStr2Num(str :STRING) :REAL; {This is more robust than Str2Num because it handles unit marks in the string, while the built-in FUNCTION does not. Returns zero if it fails, which of course does not unambiguously indicate failure, but it's the same thing that Str2Num returns on failure (without the warning). If you need to know whether or not the FUNCTION succeeded, you shouldn't be using Str2Num, but ValidNumStr instead.} VAR num :REAL; BEGIN MyStr2Num := 0; IF ValidNumStr(str, num) THEN MyStr2Num := num; END;
Example
VectorScript
numValue:=Str2Num('235.44');
Python
Version
Availability: from All Versions
See Also
VS Functions:
VS Functions:
[[VS:ValidNumStr]]