parseInt()

Discussion in 'Javascript' started by passion_to_be_free@hotmail.com, Aug 7, 2005.

  1. Guest

    So I have a table setup like this:

    <table>
    <tr>
    <td>425</td>
    </tr>
    </table>

    and I'm retrieving a value out of a table using this code:

    var x=document.getElementById('myTable').rows
    var y=x[0].cells
    var usedMin = y[0].innerHTML;

    now, I know that usedMin is a number, when I call:

    return usedMin

    I get "425". (the number I expect to get.) But for some reason, if I
    try to do any math on it, like:

    usedMin - 100
    parseInt(usedMin)

    I get the NaN error. Does anyone know why I can't type cast this as an
    integer?

    -benjamin
     
    , Aug 7, 2005
    #1
    1. Advertising

  2. Joakim Braun Guest

    <> skrev i meddelandet
    news:...
    > So I have a table setup like this:
    >
    > <table>
    > <tr>
    > <td>425</td>
    > </tr>
    > </table>
    >
    > and I'm retrieving a value out of a table using this code:
    >
    > var x=document.getElementById('myTable').rows
    > var y=x[0].cells
    > var usedMin = y[0].innerHTML;


    > now, I know that usedMin is a number, when I call:


    usedMin is not a number. It's a string. (A string with numeric characters,
    yes - but even so, a string)

    > return usedMin
    >
    > I get "425". (the number I expect to get.) But for some reason, if I
    > try to do any math on it, like:


    You don't get the "number" you expect to get, but a string containing the
    characters "425".

    > usedMin - 100
    > parseInt(usedMin)


    "usedMin - 100" by itself is kind of meaningless. And why are you calling
    parseInt(usedMin) after that?

    parseInt() returns the first integer of the string argument passed in.
    So you could do:

    usedMin = parseInt(usedMin);

    if(!isNaN(usedMin)){

    // Do your calculations
    }

    > I get the NaN error. Does anyone know why I can't type cast this as an
    > integer?


    In the code you've shown, you haven't used parseInt() in a way that enables
    you to do calculations with usedMin.

    --
    Joakim Braun
     
    Joakim Braun, Aug 7, 2005
    #2
    1. Advertising

  3. Lee Guest

    said:
    >
    >So I have a table setup like this:
    >
    ><table>
    ><tr>
    ><td>425</td>
    ></tr>
    ></table>
    >
    >and I'm retrieving a value out of a table using this code:
    >
    >var x=document.getElementById('myTable').rows
    >var y=x[0].cells
    >var usedMin = y[0].innerHTML;
    >
    >now, I know that usedMin is a number, when I call:
    >
    >return usedMin
    >
    >I get "425". (the number I expect to get.) But for some reason, if I
    >try to do any math on it, like:
    >
    >usedMin - 100
    >parseInt(usedMin)
    >
    >I get the NaN error. Does anyone know why I can't type cast this as an
    >integer?


    Please don't waste our time by posting code that's sorta kinda
    like the general idea of the code that's not working for you.

    You're likely to get responses pointing out that you haven't set
    the id attribute of your table and that "usedMin - 100" is a no-op.

    Instead, build the simplest test case that shows your problem.
    You'll often solve your own problem while doing this, and if
    you don't, you'll make it easier for people who might be willing
    to help you. In the meantime, take a look at this code, which
    works in Firefox and IE and see what's different from your code.
    I'm betting that you've got some HTML markup in the cell in
    addition to "425":

    <html>
    <head>
    <script type="text/javascript">
    function foo() {
    var x=document.getElementById('myTable').rows
    var y=x[0].cells
    var usedMin = y[0].innerHTML;
    usedMin-=100;
    alert(parseInt(usedMin));
    }
    </script>
    </head>
    <body>
    <table id="myTable">
    <tr>
    <td>425</td>
    </tr>
    </table>
    <button onclick="foo()">foo</button>
    </body>
    </html>
     
    Lee, Aug 7, 2005
    #3
  4. Lee Guest

    Lee said:
    >
    > said:
    >>
    >>So I have a table setup like this:
    >>
    >><table>
    >><tr>
    >><td>425</td>
    >></tr>
    >></table>
    >>
    >>and I'm retrieving a value out of a table using this code:
    >>
    >>var x=document.getElementById('myTable').rows
    >>var y=x[0].cells
    >>var usedMin = y[0].innerHTML;
    >>
    >>now, I know that usedMin is a number, when I call:
    >>
    >>return usedMin
    >>
    >>I get "425". (the number I expect to get.) But for some reason, if I
    >>try to do any math on it, like:
    >>
    >>usedMin - 100
    >>parseInt(usedMin)
    >>
    >>I get the NaN error. Does anyone know why I can't type cast this as an
    >>integer?

    >
    >Please don't waste our time by posting code that's sorta kinda
    >like the general idea of the code that's not working for you.
    >
    >You're likely to get responses pointing out that you haven't set
    >the id attribute of your table and that "usedMin - 100" is a no-op.
    >
    >Instead, build the simplest test case that shows your problem.
    >You'll often solve your own problem while doing this, and if
    >you don't, you'll make it easier for people who might be willing
    >to help you. In the meantime, take a look at this code, which
    >works in Firefox and IE and see what's different from your code.
    >I'm betting that you've got some HTML markup in the cell in
    >addition to "425":
    >
    ><html>
    ><head>
    ><script type="text/javascript">
    >function foo() {
    > var x=document.getElementById('myTable').rows
    > var y=x[0].cells
    > var usedMin = y[0].innerHTML;
    > usedMin-=100;
    > alert(parseInt(usedMin));


    And I forgot to point out that using parseInt() after subtracting
    100 is a complete waste, since the subtraction converts the string
    to a numeric value. Passing a numeric value to parseInt() forces
    it to cast the value back into a string, then parse out the integer
    value.
     
    Lee, Aug 7, 2005
    #4
  5. JRS: In article <>
    , dated Sun, 7 Aug 2005 09:42:40, seen in news:comp.lang.javascript,
    posted :
    >So I have a table setup like this:
    >
    ><table>
    ><tr>
    ><td>425</td>
    ></tr>
    ></table>
    >
    >and I'm retrieving a value out of a table using this code:
    >
    >var x=document.getElementById('myTable').rows
    >var y=x[0].cells
    >var usedMin = y[0].innerHTML;
    >
    >now, I know that usedMin is a number, when I call:
    >
    >return usedMin
    >
    >I get "425". (the number I expect to get.) But for some reason, if I
    >try to do any math on it, like:
    >
    >usedMin - 100
    >parseInt(usedMin)
    >
    >I get the NaN error. Does anyone know why I can't type cast this as an
    >integer?



    NaN is not, of itself, an error. Either it is one of the possible
    values of a javascript Number, or it is one of an indistinguishable set
    of such values (other languages can distinguish members of the set).

    One reason why you cannot typecast usedMin as an integer is that current
    javascript has no such type as integer. You should be able to use
    Number(usedMin) though. Note : parseInt() is not a typecast.

    Your prime error seems to be that you have not studied the newsgroup
    FAQ.

    Using parseInt is generally unnecessary (use it if there may be other
    characters after the numeric part); and, when it is used, it should
    generally be used with a second parameter, commonly 10.

    To test whether usedMin is a Number, use alert(typeof usedMin).

    If it is certain that the contents of innerHTML will be a numeric
    (decimal) string (it will of course be of type String), then all you
    should need is return +usedMin . Otherwise, validate (see sig
    line 3) or be prepared for NaN.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 8, 2005
    #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. Sam Iam
    Replies:
    6
    Views:
    3,991
    satdmail
    Jul 19, 2006
  2. Knute Johnson

    Interesting Integer.parseInt() problem

    Knute Johnson, Jan 31, 2006, in forum: Java
    Replies:
    4
    Views:
    12,477
    Roedy Green
    Jan 31, 2006
  3. Paul Rubin
    Replies:
    3
    Views:
    708
    jose isaias cabrera
    Feb 1, 2005
  4. Bill Mill
    Replies:
    0
    Views:
    363
    Bill Mill
    Feb 1, 2005
  5. jose isaias cabrera

    Re: Java Integer.ParseInt translation to python

    jose isaias cabrera, Feb 1, 2005, in forum: Python
    Replies:
    0
    Views:
    573
    jose isaias cabrera
    Feb 1, 2005
Loading...

Share This Page