473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

parseInt()

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
5 10338
<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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Thies | last post by:
I've noticed that doing: var x=any number between -1 and 1, like 0.5 parsInt(x) yields NaN. I was expecting zero. Is there another way of doing this?
9
by: Henrik | last post by:
In Java you can write something like this. Does anyone know how to do this in javascript? "byte b=Integer.parseInt(int value or String).byteValue;"
14
by: jacster | last post by:
Hi, I'm trying to parse a string of the form 08:00 representing a time so I can calculate the difference between two times. parseInt(time) with a leading zero returns 0. Is there a way around...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
25
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does parseInt('09') give an error? ----------------------------------------------------------------------- ...
14
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- ...
12
by: kilik3000 | last post by:
Why does parseInt("0000000000000018") return 1, while parseInt("0000000000000018", 10) return 18? My assumption was that the base 10 would be default argument for radix. Wouldn't you want to...
6
by: lorlarz | last post by:
Regarding http://mynichecomputing.com/digitallearning/yourOwn.htm and how to make it fail when it should not with an integer OR parseInt to integer conversion problem. THE real problem IS is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.