Convert Boolean to Numeric

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pachydermitis

    #1

    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
  • kaeli

    #2
    Re: Convert Boolean to Numeric

    In article <4f954dcf.03100 11104.670bb6c8@ posting.google. com>,
    dedejavu@hotmai l.com enlightened us with...[color=blue]
    > 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
    >[/color]

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


    -------------------------------------------------
    ~kaeli~
    All I ask for is the chance to prove that money
    cannot make me happy.


    -------------------------------------------------

    Comment

    • Lee

      #3
      Re: Convert Boolean to Numeric

      kaeli said:[color=blue]
      >
      >In article <4f954dcf.03100 11104.670bb6c8@ posting.google. com>,
      >dedejavu@hotma il.com enlightened us with...[color=green]
      >> 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
      >>[/color]
      >
      >var b = true;
      >var x= b?1:0;[/color]

      Even simpler:
      var x = +b;

      The unary plus converts its operand to a number.

      Comment

      • VK

        #4
        Re: Convert Boolean to Numeric

        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 <dedejavu@hotma il.com> wrote in message
        news:4f954dcf.0 310011104.670bb 6c8@posting.goo gle.com...[color=blue]
        > 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[/color]


        Comment

        • Dr John Stockton

          #5
          Re: Convert Boolean to Numeric

          JRS: In article <3f7b374b$0$128 58$9b622d9e@new s.freenet.de>, seen in
          news:comp.lang. javascript, VK <schools_ring@y ahoo.com> posted at Wed, 1
          Oct 2003 22:20:59 :-[color=blue]
          >JavaScript is not Java, its casting syntax is narrowed.
          >As 'kaeli' said, the simplest is:
          >myElement.valu e = (x)?1:0;
          >And what is not elegant in this?
          >
          >Pachydermiti s <dedejavu@hotma il.com> wrote in message
          >news:4f954dcf. 0310011104.670b b6c8@posting.go ogle.com...[color=green]
          >> 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.[/color][/color]

          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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          • Emil Mroz

            #6
            Re: Convert Boolean to Numeric

            On Thu, 2 Oct 2003 13:02:42 +0100, Dr John Stockton
            <spam@merlyn.de mon.co.uk> wrote:
            [color=blue]
            >JRS: In article <3f7b374b$0$128 58$9b622d9e@new s.freenet.de>, seen in
            >news:comp.lang .javascript, VK <schools_ring@y ahoo.com> posted at Wed, 1
            >Oct 2003 22:20:59 :-[color=green]
            >>JavaScript is not Java, its casting syntax is narrowed.
            >>As 'kaeli' said, the simplest is:
            >>myElement.val ue = (x)?1:0;
            >>And what is not elegant in this?
            >>[/color]
            >Since +x will do the conversion, your method is tediously long-winded
            >and untidy. It is certainly not simplest.[/color]

            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

            Comment

            Working...