By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

parseInt()

P: n/a
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 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<pa****************@hotmail.com> skrev i meddelandet
news:11**********************@o13g2000cwo.googlegr 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

Aug 7 '05 #2

P: n/a
Lee
pa****************@hotmail.com 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>

Aug 7 '05 #3

P: n/a
Lee
Lee said:

pa****************@hotmail.com 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.

Aug 7 '05 #4

P: n/a

The .innerHTML property returns a 'string' object, not a numeric one, so,
you have to 1st convert it to numeric in order to use it as numeric, a
simple way in js is by multiplying by 1 :)

'425'*1 gives 425
Now, you also can use parseInt('425')-100 or Number('425')-100, to do
numeric operations.
Danny
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 7 '05 #5

P: n/a
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Sun, 7 Aug 2005 09:42:40, seen in news:comp.lang.javascript,
pa****************@hotmail.com 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.
Aug 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.