Velocity Reviews > Convert Boolean to Numeric

# Convert Boolean to Numeric

Pachydermitis
Guest
Posts: n/a

 10-01-2003
Hi all,
This has got to be easy, but it's got me going bald.
How do I convert a Boolean to a numberic value? This value is going
to a hidden field that is going to an SQL server. There has to be an
elegant way to change true to 1 and false to 0.
Thanks
Pachydermitis

kaeli
Guest
Posts: n/a

 10-01-2003
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with...
> Hi all,
> This has got to be easy, but it's got me going bald.
> How do I convert a Boolean to a numberic value? This value is going
> to a hidden field that is going to an SQL server. There has to be an
> elegant way to change true to 1 and false to 0.
> Thanks
> Pachydermitis
>

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

-------------------------------------------------
~kaeli~
All I ask for is the chance to prove that money
cannot make me happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
-------------------------------------------------

Lee
Guest
Posts: n/a

 10-01-2003
kaeli said:
>
>In article <(E-Mail Removed) >,
>(E-Mail Removed) enlightened us with...
>> Hi all,
>> This has got to be easy, but it's got me going bald.
>> How do I convert a Boolean to a numberic value? This value is going
>> to a hidden field that is going to an SQL server. There has to be an
>> elegant way to change true to 1 and false to 0.
>> Thanks
>> Pachydermitis
>>

>
>var b = true;
>var x= b?1:0;

Even simpler:
var x = +b;

The unary plus converts its operand to a number.

VK
Guest
Posts: n/a

 10-01-2003
JavaScript is not Java, its casting syntax is narrowed.
As 'kaeli' said, the simplest is:
myElement.value = (x)?1:0;
And what is not elegant in this?

Pachydermitis <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi all,
> This has got to be easy, but it's got me going bald.
> How do I convert a Boolean to a numberic value? This value is going
> to a hidden field that is going to an SQL server. There has to be an
> elegant way to change true to 1 and false to 0.
> Thanks
> Pachydermitis

Dr John Stockton
Guest
Posts: n/a

 10-02-2003
JRS: In article <3f7b374b\$0\$12858\$(E-Mail Removed)>, seen in
news:comp.lang.javascript, VK <(E-Mail Removed)> posted at Wed, 1
Oct 2003 22:20:59 :-
>JavaScript is not Java, its casting syntax is narrowed.
>As 'kaeli' said, the simplest is:
>myElement.value = (x)?1:0;
>And what is not elegant in this?
>
>Pachydermitis <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed). com...
>> Hi all,
>> This has got to be easy, but it's got me going bald.
>> How do I convert a Boolean to a numberic value? This value is going
>> to a hidden field that is going to an SQL server. There has to be an
>> elegant way to change true to 1 and false to 0.

Since +x will do the conversion, your method is tediously long-winded
and untidy. It is certainly not simplest.

Please read the weekly-posted FAQ on the composition of news replies; in
particular on the desirability of trimming quotes and answering after.

Look at the posts in this group; to avoid possible prejudice, omit yours
and mine. You will soon see that the more competent posters use the
form recommended in the FAQ; and, ISTM, that such posters get better
replies, if they need help.

--
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.