Convert Boolean to Numeric

Discussion in 'Javascript' started by Pachydermitis, Oct 1, 2003.

  1. 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
    Pachydermitis, Oct 1, 2003
    #1
    1. Advertising

  2. Pachydermitis

    kaeli Guest

    In article <>,
    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
    -------------------------------------------------
    kaeli, Oct 1, 2003
    #2
    1. Advertising

  3. Pachydermitis

    Lee Guest

    kaeli said:
    >
    >In article <>,
    > 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.
    Lee, Oct 1, 2003
    #3
  4. Pachydermitis

    VK Guest

    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 <> wrote in message
    news:...
    > 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
    VK, Oct 1, 2003
    #4
  5. JRS: In article <3f7b374b$0$12858$>, seen in
    news:comp.lang.javascript, VK <> 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 <> wrote in message
    >news:...
    >> 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.
    Dr John Stockton, Oct 2, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vighnesh
    Replies:
    3
    Views:
    1,023
    Paul Clement
    Aug 4, 2005
  2. J Leonard
    Replies:
    4
    Views:
    12,620
    Mark Space
    Jan 19, 2008
  3. vighnesh
    Replies:
    3
    Views:
    175
    Paul Clement
    Aug 4, 2005
  4. Jason Lillywhite

    numeric boolean check

    Jason Lillywhite, Mar 17, 2009, in forum: Ruby
    Replies:
    1
    Views:
    111
    Saji N. Hameed
    Mar 17, 2009
  5. Metre Meter
    Replies:
    7
    Views:
    354
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page