What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Convert Boolean to Numeric?

Pages: (2): « First « 1 [ 2 ] Last »
Convert Boolean to Numeric?
Author: Message:
ShawnZ
Veteran Member
*****

Avatar

Posts: 3146
Reputation: 43
32 / Male / Flag
Joined: Jan 2003
RE: Convert Boolean to Numeric?
quote:
Originally posted by markee
quote:
Originally posted by phalanxii
I believe Math.abs(<boolean>) also does that.
This will give the absolute value of the number which is not what you want.  Boolean expressions are equivalent to 1 for true, 0 for null and -1 for false and thus you would have a problem where you couldn't tell the difference between true and false.  Even Spunky's code is incorrect because it misses the null possibility and false should be negate one not zero.  The proper methods would be either to use the parseInt method or multiply the boolean variable by 1 (or minus zero).  This is also the same when you try to convert a string to a number.

....multiplying -1 by 1 wouldn't produce 0 as he wanted, under your logic. false is equal to 0.
Spoiler:
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
the game.
10-04-2007 02:04 AM
Profile PM Web Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
39 / Male / Flag
Joined: Sep 2004
O.P. RE: Convert Boolean to Numeric?
Hmm, this has turned into quite a discussion. To clarify what I was using this for, I have decided to make a GUI for my script, and I am using a few checkboxes. Of course, they have a true or false value, and I wanted to pass the values as numeric so that they would work with the existing scripting I had already done.  As I stated before, this now works using the code SpunkyLoveMuff provided
code:
myVar = myVar?1:0;
This seems to do exactly what I was looking for, I just need to use it for each checkbox that I have. Initially, I had tried multiplying the boolean value by 1 which didnt seem to do anything for me. I havent tried any of the other methods mentioned, as they first one worked for me :)
10-04-2007 03:04 AM
Profile E-Mail PM Web Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Convert Boolean to Numeric?
There is no need to actual convert between boolean and integer from what I can see... You can even set the checkboxes useing 1 or 0 instead of true or false
<Eljay> "Problems encountered: shit blew up" :zippy:
10-04-2007 11:51 AM
Profile PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
36 / Male / Flag
Joined: Mar 2004
RE: Convert Boolean to Numeric?
quote:
Originally posted by markee
quote:
Originally posted by phalanxii
I believe Math.abs(<boolean>) also does that.
This will give the absolute value of the number which is not what you want.  Boolean expressions are equivalent to 1 for true, 0 for null and -1 for false and thus you would have a problem where you couldn't tell the difference between true and false.  Even Spunky's code is incorrect because it misses the null possibility and false should be negate one not zero.  The proper methods would be either to use the parseInt method or multiply the boolean variable by 1 (or minus zero).  This is also the same when you try to convert a string to a number.
er what boolean is either 1 (true) OR 0 (false) theres no -1, its its null its not a boolean, its null :-/

Math.abs does exactly what he wants :-/

code:
Math.abs(true) == 1
Math.abs(false) == 0
Math.abs(null) == 0
Math.abs(undefined) == NaN

[Image: dt2.0v2.png]      Happy Birthday, WDZ
10-04-2007 02:21 PM
Profile PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Convert Boolean to Numeric?
Why do you all make it so difficult? Multiplying a boolean with 1 won't cause any troubles and is the easiest method to use! :grin:

It simply depends on your preference. I just like the multiplying method, but the other ways will do just the same. It's up to you! :)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
10-04-2007 04:52 PM
Profile E-Mail PM Web Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1622
Reputation: 50
36 / Male / Flag
Joined: Jan 2006
RE: Convert Boolean to Numeric?
quote:
Originally posted by -dt-
quote:
Originally posted by markee
quote:
Originally posted by phalanxii
I believe Math.abs(<boolean>) also does that.
This will give the absolute value of the number which is not what you want.  Boolean expressions are equivalent to 1 for true, 0 for null and -1 for false and thus you would have a problem where you couldn't tell the difference between true and false.  Even Spunky's code is incorrect because it misses the null possibility and false should be negate one not zero.  The proper methods would be either to use the parseInt method or multiply the boolean variable by 1 (or minus zero).  This is also the same when you try to convert a string to a number.
er what boolean is either 1 (true) OR 0 (false) theres no -1, its its null its not a boolean, its null :-/

Math.abs does exactly what he wants :-/

code:
Math.abs(true) == 1
Math.abs(false) == 0
Math.abs(null) == 0
Math.abs(undefined) == NaN


My bad, I always thought it was -1.  Would make more sense to differentiate null and false IMO :P
[Image: markee.png]
10-04-2007 10:41 PM
Profile PM Find Quote Report
Voldemort
Veteran Member
*****

Avatar

Posts: 3504
Reputation: 49
– / – / Flag
Joined: Jul 2005
Status: Away
RE: Convert Boolean to Numeric?
i've never seen null in any boolean anywhere :p
*All posts are a purely speculative hypothesis based on abstract reasoning.
Not my daughter, you bitch!
[Image: ico-mollytrix16.gif]
10-04-2007 11:01 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Convert Boolean to Numeric?
quote:
Originally posted by Voldemort
i've never seen null in any boolean anywhere :p

quote:
Originally posted by Google
A logical truth value. The two possible Boolean values are true and false .

Simple answer... Null isn't boolean (I know you know that :P)

quote:
Originally posted by Google
A null value tells you the value for that row is either missing, unknown, not yet know or inapplicable.


This post was edited on 10-05-2007 at 12:43 AM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
10-05-2007 12:40 AM
Profile PM Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On