473,412 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Convert Boolean to Numeric

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
Jul 20 '05 #1
5 9509
In article <4f**************************@posting.google.com >,
de******@hotmail.com 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
-------------------------------------------------
Jul 20 '05 #2
Lee
kaeli said:

In article <4f**************************@posting.google.com >,
de******@hotmail.com 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.

Jul 20 '05 #3
VK
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 <de******@hotmail.com> wrote in message
news:4f**************************@posting.google.c 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

Jul 20 '05 #4
JRS: In article <3f***********************@news.freenet.de>, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> 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 <de******@hotmail.com> wrote in message
news:4f**************************@posting.google. 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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
On Thu, 2 Oct 2003 13:02:42 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <3f***********************@news.freenet.de>, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> 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?

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


The +x trick might be simpler, but I find that x?1:0 is much easier to
read and comprehend. On the outset, +x looks like some sort of syntax
error. Simpler is not always better when one is writing new code.

I seem to remember reading a quote somewhere about "premature
optimization".

-- Emil
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
0
by: Frank van Vugt | last post by:
Hi, FWIW, I walked into this one when changing an int into a numeric(5,1) : IF ( 0::int ) => interpreted as false IF ( 0.0::numeric(5,1) ) => interpreted as true
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
4
by: simonZ | last post by:
Why this don't work: Boolean test; String testValue; testValue="0"; test=System.Convert.ToBoolean(testValue); How can I convert string to boolean?
270
by: Jordan | last post by:
Hi everyone, I'm a big Python fan who used to be involved semi regularly in comp.lang.python (lots of lurking, occasional posting) but kind of trailed off a bit. I just wrote a frustration...
19
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.