Shoutbox

Convert Boolean to Numeric? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Convert Boolean to Numeric? (/showthread.php?tid=77950)

Convert Boolean to Numeric? by stu on 10-03-2007 at 07:18 PM

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?


RE: Convert Boolean to Numeric? by Spunky on 10-03-2007 at 07:22 PM

Like this:
var b = true;
var x= b?1:0;


RE: Convert Boolean to Numeric? by stu on 10-03-2007 at 07:42 PM

ok, thanks. Im now using

code:
myVar = myVar?1:0;
A little bit shorter than my 5 lines I was previously using :)
RE: Convert Boolean to Numeric? by markee on 10-03-2007 at 11:52 PM

code:
var myNumber = parseInt(myBoolean);
You could always multiply it by 1 or something as well.
RE: Convert Boolean to Numeric? by phalanxii on 10-04-2007 at 12:33 AM

I believe Math.abs(<boolean>) also does that.


RE: Convert Boolean to Numeric? by markee on 10-04-2007 at 12:44 AM

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.
RE: Convert Boolean to Numeric? by phalanxii on 10-04-2007 at 01:05 AM

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.
RE: Convert Boolean to Numeric? by deAd on 10-04-2007 at 01:22 AM

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).


RE: Convert Boolean to Numeric? by phalanxii on 10-04-2007 at 01:32 AM

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.


RE: Convert Boolean to Numeric? by deAd on 10-04-2007 at 01:37 AM

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();
}

RE: Convert Boolean to Numeric? by ShawnZ on 10-04-2007 at 02:04 AM

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.
RE: Convert Boolean to Numeric? by stu on 10-04-2007 at 03:04 AM

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 :)
RE: Convert Boolean to Numeric? by Spunky on 10-04-2007 at 11:51 AM

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


RE: Convert Boolean to Numeric? by -dt- on 10-04-2007 at 02:21 PM

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


RE: Convert Boolean to Numeric? by Matti on 10-04-2007 at 04:52 PM

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! :)


RE: Convert Boolean to Numeric? by markee on 10-04-2007 at 10:41 PM

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
RE: Convert Boolean to Numeric? by Voldemort on 10-04-2007 at 11:01 PM

i've never seen null in any boolean anywhere :p


RE: Convert Boolean to Numeric? by Spunky on 10-05-2007 at 12:40 AM

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.