You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today I wrote some code to do extract a number from a given string.
For stuff like "[whitespace]1.2345[whitespace]" this works fine and converts fasts.
But in my case I want to compare a "(potentially) numeric string" with a given number (long, double, ...).
The current implementation of string.ToInt() and the likes do not tell the user, if the input string was number at all.
Is there a way to add some parameter to "ToInt..." (like an overload) to retrieve if the input string contained something "else" than a number (so AZaz ... and the likes?
For now to achieve this one needs to check if the resulting number has the same "string representation" as the input string (trimmed)
Local d:Double = double(s)
If Abs(d) < 0.000001 '"hello world" became 0-check required
if string(d) = s.trim() Then print "invalid number"
Else
print "number is " + d
EndIf
As this is done in BlitzMax, we create a new string (string(d)) and in case of an invalid conversion (Double("hello")) another one.
This is overhead I would like to seen avoided (aka an option is provided to see if a provided converted number bases on a valid "string number").
The text was updated successfully, but these errors were encountered:
The new code contains "ToDoubleEx()" and the likes - which add further options and configurability.
Yet it still fails with values like 12asdsfsdf-32432" (ToLongEx()andlong(string)extract "12" here... which is incorrect). TolongEx() returns the position of the first non-long char (here the "a")) but it also returns the first whitespace position for"12 "` so you do not know if the given number is a valid long with whitespace, or just a alpha-num string which begun with numbers.
My "comparative" approaches use log10 (but there whitespace is not ignored) or ExtractNumber():
SuperStrict
Framework Brl.StandardIO
Import Brl.Retro
Local s:String = "12a-a1234"
Local l:Long = Long(s)
Local digits:Int
If l > 0
digits = Int(Log10(l)) + 1
ElseIf l = 0
digits = 1
Else
digits = Int(Log10(-l)) + 2
EndIf
If digits <> s.Length
Print "invalid long: " + s + " -> " + l
Else
Print "valid long :" + s + " -> " + l
EndIf
Local l2:Long
If ExtractNumber(s, l2)
Print "valid long : " + l2
Else
Print "invalid long : " + s
EndIf
Function ExtractNumber:Int(s:String, longValue:Long Var)
If s.Length = 0 Then Return 0 'no extraction happened
longValue = 0
Local negative:Int = 0
Local decimalDivider:Long=10
Local hasSpaceAfter:Int = 0
Local hasDigits:Int = 0
Local hasMinus:Int = 0
Local index:Int = 0
While index < s.Length
Local charCode:Int = s[index]
'only allow spaces once a space after a numeric value happened
If hasSpaceAfter And charCode <> Asc(" ")
Return 0 'invalid
EndIf
'extract number / decimals
If (charCode >= 48 And charCode <= 57) '48 = "0", 57 = "9"
longValue = longValue * 10 + (charCode-48)
hasDigits = True
' allow minus at begin
ElseIf charCode = Asc("-") And Not hasDigits
hasMinus = True
' allow space at begin and end
ElseIf charCode = Asc(" ")
' if space at end - mark it
If hasDigits
hasSpaceAfter = True
EndIf
' invalid char found
Else
Return 0 'invalid
EndIf
index :+ 1 'processed
Wend
If hasMinus
longValue = -1 * longValue
EndIf
Return 1
End Function
Heya,
Today I wrote some code to do extract a number from a given string.
For stuff like
"[whitespace]1.2345[whitespace]"
this works fine and converts fasts.But in my case I want to compare a "(potentially) numeric string" with a given number (long, double, ...).
The current implementation of
string.ToInt()
and the likes do not tell the user, if the input string was number at all.Is there a way to add some parameter to "ToInt..." (like an overload) to retrieve if the input string contained something "else" than a number (so AZaz ... and the likes?
For now to achieve this one needs to check if the resulting number has the same "string representation" as the input string (trimmed)
As this is done in BlitzMax, we create a new string (
string(d)
) and in case of an invalid conversion (Double("hello")
) another one.This is overhead I would like to seen avoided (aka an option is provided to see if a provided converted number bases on a valid "string number").
The text was updated successfully, but these errors were encountered: