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:
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. Convert Boolean to Numeric?
Is there an easy way to convert a boolean value to a numeric value?

The boolean values are either true or false, and I want it to return either 1 or 0, respectively.

Im using an if statement right now, but Im wondering if there is a simpler way?
10-03-2007 07:18 PM
Profile E-Mail PM Web Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
35 / Male / Flag
Joined: Aug 2006
RE: Convert Boolean to Numeric?
Like this:
var b = true;
var x= b?1:0;

This post was edited on 10-03-2007 at 07:23 PM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
10-03-2007 07:22 PM
Profile PM Find Quote Report
stu
Junior Member
**

Avatar
Stu

Posts: 82
Reputation: 1
38 / Male / Flag
Joined: Sep 2004
O.P. RE: Convert Boolean to Numeric?
ok, thanks. Im now using
code:
myVar = myVar?1:0;
A little bit shorter than my 5 lines I was previously using :)
10-03-2007 07:42 PM
Profile E-Mail PM Web Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: Convert Boolean to Numeric?
code:
var myNumber = parseInt(myBoolean);
You could always multiply it by 1 or something as well.

This post was edited on 10-03-2007 at 11:53 PM by markee.
[Image: markee.png]
10-03-2007 11:52 PM
Profile PM Find Quote Report
phalanxii
Full Member
***


Posts: 146
Reputation: 5
32 / Male / Flag
Joined: Aug 2006
Status: Away
RE: Convert Boolean to Numeric?
I believe Math.abs(<boolean>) also does that.
10-04-2007 12:33 AM
Profile PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: Convert Boolean to Numeric?
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.
[Image: markee.png]
10-04-2007 12:44 AM
Profile PM Find Quote Report
phalanxii
Full Member
***


Posts: 146
Reputation: 5
32 / Male / Flag
Joined: Aug 2006
Status: Away
RE: Convert Boolean to Numeric?
As a matter of fact, null is not a Boolean data type, but its own special 'null' data type.

According to MSDN:
quote:
Whereas the string and number data types can have a virtually unlimited number of different values, the Boolean data type can only have two. They are the literals true and false.
And in ordinary computing, true is 1 and false is 0, which is exactly what Math.abs() and Spunky's code return for those respective values.

Also, the parseInt() method will return NaN if you pass a Boolean into it. However, multiplying a Boolean by 1 works.

This post was edited on 10-04-2007 at 01:07 AM by phalanxii.
10-04-2007 01:05 AM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Convert Boolean to Numeric?
If the only reason you are doing this is to pass to a WinAPI or DLL function, just pass the boolean and it will be converted automatically by Plus. Otherwise, just use parseInt, multiply by one, or an inline if statement (b?1:0).
10-04-2007 01:22 AM
Profile PM Find Quote Report
phalanxii
Full Member
***


Posts: 146
Reputation: 5
32 / Male / Flag
Joined: Aug 2006
Status: Away
RE: Convert Boolean to Numeric?
parseInt does not work for Booleans. It only works for strings. Passing a Boolean into it will result in NaN (not a number). Just do a simple debug trace and you will see.
10-04-2007 01:32 AM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Convert Boolean to Numeric?
Personally I would use an inline if statement, as the other methods could potentially give you a -1 as your number.

You could even make it a function of the boolean object (but this is sort of overkill):
code:
Boolean.prototype.toInt = function () { return this ? 1 : 0; }

// to use it, just call the toInt member of a boolean object
function doSomethingWithBooleans() {
var bMyBoolean = true;
var nBoolAsInteger = bMyBoolean.toInt();
}

This post was edited on 10-04-2007 at 01:41 AM by deAd.
10-04-2007 01:37 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