Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > parseInt()

Reply
Thread Tools

parseInt()

 
 
passion_to_be_free@hotmail.com
Guest
Posts: n/a
 
      08-07-2005
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

 
Reply With Quote
 
 
 
 
Joakim Braun
Guest
Posts: n/a
 
      08-07-2005
<> skrev i meddelandet
news: oups.com...
> 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



 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      08-07-2005
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>

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      08-07-2005
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.

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-08-2005
JRS: In article < .com>
, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57