Sign in to follow this  
Followers 0
Valik

Conversion from string to number.

   44 members have voted

  1. 1. Which of the following do you expect?

    • The expression Number("5abc") should return 0.
      34
    • The expression Number("5abc") should return 5.
      10

Please sign in or register to vote in this poll.

42 posts in this topic

#1 ·  Posted (edited)

This poll is to gauge what user expectations are for the conversion from a string to a number when the string starts with numbers but also contains non-numbers. I do not care about what the current behavior is, I am only interested in what the expected behavior from you in the community is.

Edit: The poll will be open for a 3 or 4 days.

Edited by Valik

Share this post


Link to post
Share on other sites



I voted for return 0 and would also like to add that I think that @error should be set.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

I expect that a number containing anything except +- or the decimal point would be treated as a string

$1234.56 = String

1,234.56 = String

-1234.56 = Number

+1234.56 = Number

As far as I am aware that's the current behaviour and I'm not sure if changing that would slow it down or not. I assume it would because the more decisions that have to be made the longer it should take.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

At first I thought, "5apples" should return 5, because it's quantifying 5 apples. But then, it doesn't consider what you're quantifying, just the quantity. In this case, the "what" matters just as much as how many, so:

Number("5apples") + Number("5oranges") = 10 ;10piecesOfFruit?!

It doesn't seem to make sense to have it return the number, because there's no telling what the intent is, and AutoIt can't be expected to invent something like piecesOfFruit on the spot.

Share this post


Link to post
Share on other sites

I'm for 0, no @error.

I expect that a number containing anything except +- or the decimal point would be treated as a string

$1234.56 = String

1,234.56 = String

-1234.56 = Number

+1234.56 = Number

As far as I am aware that's the current behaviour and I'm not sure if changing that would slow it down or not. I assume it would because the more decisions that have to be made the longer it should take.

Scientific notation?

Btw, see this:

; Case 1
$sNumber = "2E5"
ConsoleWrite(Number($sNumber) & @CRLF)

; Case 1
$sNumber = "2E5"
ConsoleWrite(($sNumber + 123) & @CRLF)


ConsoleWrite(@CRLF)

; Case 3
$sNumber = "2.4E5"
ConsoleWrite(Number($sNumber) & @CRLF)

; Case 4
$sNumber = "2.4E5"
ConsoleWrite(($sNumber + 123) & @CRLF)


ConsoleWrite(@CRLF)

; Case 5
$sNumber = "240000"
ConsoleWrite(($sNumber + 123) & @CRLF)

Degree of inconsistency maybe?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

I think it should be 0 because I think of it like 5*a*b*c and you haven't set a b or c yet so they should be null or zero so 5*0*0*0=0

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I would expect it to work exactly as if you tried to perform a number function on it... (A non numeric type = 0. This retains any floating points, + and - etc.

; test 1

$sNum = "1234"

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))

$sNum += 0

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))



; test 2

$sNum = "5Apples"

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))

$sNum += 0

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))


; test 3

$sNum = "-5.6546"

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))

$sNum += 0

MsgBox (0, $sNum, "Is Str? " & @TAB & IsString ($sNum) & @CRLF & "Is Num? " & @TAB & IsNumber ($sNum))

basically...

Func _Number ($sString)
   Return $sString + 0
EndFunc   ;==>_Number

Mat

Edited by Mat

Share this post


Link to post
Share on other sites

I agree with monoceres, return 0 and set @error.

Share this post


Link to post
Share on other sites

 Personally I would like to see the Number() function work in a similar way to VBs Val() function. 

@error should be used to indicate if the string could successfully be interpreted as a number and @extended used to indicate if the string contained non-numeric characters.

When the Number function is used the user is making a conscious decision to try and convert a string to a number and therefore should take responsibility for checking that it worked   

Examples:

Number("5abc")        ; return 5 @error set to 0 @extended set to 1
Number("2457")        ; return 2457 @error set to 0 @extended set to 0
Number(" 2 45 7")     ; return 2457 @error set to 0 @extended set to 1 - ignore whitespace
Number("1,123,456.123") ; return 1123456.123 @error set to 0 @extended set to 1 - ignore commas
Number("£12.00")        ; return 12.00 @error set to 0 @extended set to 1 - ignore currency symbols
Number("a57")           ; return 0 @error set to 1 @extended set to 0
Number("24 and 57")     ; return 24 @error set to 0 @extended set to 1

I would not expect AutoIts implicit conversions to work in the same way i.e. 4 + "5abc" should = 4 and not 9 


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Why should Number(" 2 45 7") work, and Number("a57") not work? Both a whitespace and 'a' are non-numeric characters. You either have to strip both or neither.

Share this post


Link to post
Share on other sites

Guys, you're getting a bit off topic. I only want to know what the expected result is from the conversion performed in the poll question. Things like @error and @extended have nothing to do with what I'm asking.

Share this post


Link to post
Share on other sites

Zero is what I would expect to be the return. If there is doubt on what a string would contain then it should be managed by the coder.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I'm going to vote for 0 as well, but not 100 satisfied with my choice.

It is true that having Number('5abc') return 5 can be handy in many "duct tape programming" real-world situations. It also behaves like atoi()/atof() and friends.

After some time chewing the question, I vote for 0 but I find it necessary to offer a two-parameter Number($str, [$option]) that would allow numeric conversion even when a suffix is present.

Implicit conversion in If '5abc' > 3 Then ... would be performed strictly as If Number('5abc') > 3 Then ... , giving If 0 > 3 ...

Users could still explicitely force a more permissive If Number('5abc', 1) > 3 Then ... , giving If 5 > 3 ...

without resorting to extra user code for extracting a numeric. Think of RegExp to grep a float as a challenge for new users: unpractical and error prone.

My 2 cents.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Guys, please stop talking about changing how Number() works. That is off-topic. I only want opinions on what value you guys feel should be returned. That's all I care about. I don't give a shit if you think Number() should make you toast in the morning. It is completely off-topic for what I'm asking. This is not a "We're going to rewrite Number(), how do you want it to work" poll. This is a "what do you expect AutoIt to do when it converts a string containing mixed numbers and letters to a number" poll. No more, no less.

Share this post


Link to post
Share on other sites

Guys, please stop talking about changing how Number() works. That is off-topic.

OK I hope I understand better what you are asking: exclusively about implicit conversions, leaving Number as it is.

But look, your vote terms are precisely talking about "how Number should work", which made things a little ambiguous.

1. The expression Number("5abc") should return 0.

2. The expression Number("5abc") should return 5.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

jchd, no, my vote options are asking what you expect Number() to return. It's not asking how you want Number() to work. It's simply asking what you expect it to return with the sample input. There is a difference.

Share this post


Link to post
Share on other sites

Meh, I think the poll has served it's purpose, anyway. An overwhelming majority seem to think it should return 0. I admit this surprises me, particularly since this is a departure from current behavior. However the poll results satisfy me and I'm sure they satisfy Jon.

As for comments about implicit casting? Implicit casting and explicit casting must behave the same. Jon and I are in complete agreement on this and no matter what we do the following two lines of code will be 100% functionally equivalent:

If Number("5abc") = 5 Then MsgBox(0, "", "Same")
If "5abc" = 5 Then MsgBox(0, "", "Same")

Since the majority seem to favor a failed cast returning 0 instead of returning whatever it can figure out, then we'll be changing both Number() and the implicit cast to do so.

Share this post


Link to post
Share on other sites

Then, no question, I revert my vote to favor

Number('3xyz') --> 3

and

If '3xyz' > 1 Then ... to be 3 > 1 --> True


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

It's a little late for that and a little irrelevant as well. As it stands right now it's 19-5 in favor of returning 0. A return of 0 is consistent with pretty much every other language Jon and I tested except C and C++. We tested Python, Lua, VBS, C via atof() and C++ via stringstream (which probably just calls atof()/atoi() as well). Jon prefers returning 0 and I prefer returning what users expect. Since users seem to expect a return of 0 then that's what it shall be.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0