parseInt()

  • Thread starter passion_to_be_free
  • Start date
P

passion_to_be_free

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
 
J

Joakim Braun

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.
 
L

Lee

(e-mail address removed) 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>
 
L

Lee

Lee said:
(e-mail address removed) 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.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sun, 7 Aug 2005 09:42:40, seen in (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top