473,498 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String to number conversion

26 New Member
I've got a number set up in my document like this

Expand|Select|Wrap|Line Numbers
  1. <span id="anID">0</span>
and using javascript, I'm pulling the value of that using

Expand|Select|Wrap|Line Numbers
  1. var theValue = document.getElementById("anID");
  2. theValue.innerHTML = theValue.innerHTML + 10;
  3.  
What I'm trying to do is pull the value inside of the span tags (0), and then add it to something else (10) to spit out a final value inside of that span tag. This is being called multiple times, so it needs to pull what's in there currently, add 10 to it, and then set the innerHTML to the new value.

The problem is I can't get it to do this correctly. My first thought was the 0 it's pulling from the span tags is being pulled as a string because it would spit out something like "010" and if i added another one, say 20, to it then it would spit out "01020" and so on. I've tried using Number() and parseInt() to convert it to a number, add them, and put it back out there.

Does anyone know if I'm just doing something wrong or missing out on some great function that will take care of this? Thanks!
Oct 19 '07 #1
7 7485
acoder
16,027 Recognized Expert Moderator MVP
Try:
Expand|Select|Wrap|Line Numbers
  1. theValue.innerHTML = parseInt(theValue.innerHTML) + 10;
Oct 20 '07 #2
sdustinh
26 New Member
Thanks for the reply.

I've tried that, and for some reason it combines them like they are both strings.

Expand|Select|Wrap|Line Numbers
  1. var sz = 123;
  2. //theValue.innerHTML = 0;
  3. theValue.innerHTML = parseInt(theValue.innerHTML) + sz;
So when it combines the two, it outputs "0123" rather than just "123" and it's the same when you try to do it again and add, say 10, it will spit out "8310"

I'm not really sure what's going on. I haven't really run into this before. Thanks for the help!
Oct 22 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
Post your full code. It should definitely work.
Oct 22 '07 #4
sdustinh
26 New Member
Well, it's part of a larger script, so I'll put the relevant parts.

Expand|Select|Wrap|Line Numbers
  1. function updatePart (hd, sz, dispType) {
  2.     var totalHDspace = document.getElementById(hd + '-total');
  3.     //var totalHDspaceUp = document.getElementById(hd + '-total');
  4.  
  5.     switch (dispType) {
  6.         case "update":
  7.             totalHDspaceUp.innerHTML = parseInt(totalHDspaceUp.innerHTML) + sz;
  8.         break;
  9.  
  10.         case "create":
  11.             totalHDspace.innerHTML = parseInt(totalHDspace.innerHTML) + sz;
  12.         break;
  13.     }
  14.  
  15. }
  16.  
  17. updatePart('hdpart-1', 123, 'create');
[PHP]<dl id="part_list">
<dt>Hard Drive Space</dt>
<?php
for ($i=0; $i<count($ksServerCfg['hard_drive']); $i++) {
echo "<dd>HD#".($i+1).": <span id='hdpart-".($i+1)."-total'>0</span> GB of ".$ksServerCfg['hard_drive'][$i]." GB used</dd>";
}
?>
</dl>[/PHP]

I use PHP to loop through all of the parts I need (in this case there are three), and assign them a unique ID (hdpart-1-total, hdpart-2-total, etc). Then I call updatePart() as the user interacts with the site. It works fine as far as updating the span tag, it just doesn't update it correctly.

I thought that this may be because I was originally calling totalHDspace multiple times and didn't give it a unique name, so I have two of them up there. I've used alert() to watch the different variables as they go through the function, and it all works fine until it tries to pull the innerHTML and add it to sz.

Oh, when I do this...

Expand|Select|Wrap|Line Numbers
  1. updatePart('hdpart-1', 123, 'create');
  2. alert(parseInt(totalHDspace.innerHTML) + sz);
I get '0123'
Oct 22 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
I've just tested your code and it works fine. Which browser are you using?

Perhaps, you can provide a test link to the whole page. If not, when and how do you call updatePart()?
Oct 22 '07 #6
sdustinh
26 New Member
Expand|Select|Wrap|Line Numbers
  1. totalHDspace.innerHTML = parseInt(totalHDspace.innerHTML) + parseInt(sz)
Because I was using a varible to pass it over, I had to add a parseInt() to that as well. I was getting random numbers from it because it was trying to convert it to hex or something.

Thanks for the help!
Oct 22 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
That's something to always look out for in future.

Glad to hear that you got it working.
Oct 22 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1567
by: 18k11tm001 | last post by:
I am reading an ASCII data file and converting some of the strings to integers or floats. However, some of the data is corrupted and the conversion doesn't work. I know that I can us exceptions,...
2
2208
by: Miki Tebeka | last post by:
Hello All, I'm shipping an application using py2exe. With Python2.3 it worked fine but when switching to Python2.4 I started getting "warning: string/unicode conversion" all over the place. ...
0
1630
by: Eugene | last post by:
I'm working on a program which needs to convert a string to various data types. I created a function using string streams and templates to do just that: template<typename T> bool...
3
2610
by: Alessandro Monopoli | last post by:
Hi all! Do you know if there's a native C++ way to convert an STL string containing a number into a real "int" or "float" number? Is quite not comfortable to use "itoa" or "atoi" or the...
4
47532
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
3
7869
by: Water Cooler v2 | last post by:
What is the .NET equivalant of the VB function String(Number as Long, Character)? I am forgetting it at the moment.
4
9437
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
1
5687
by: PAF | last post by:
Hi, I'm trying to evaluate a sum expression trougth .NET 2 System.Xml.XPath.XPathNavigator object. My code is : Dim document As New System.Xml.XmlDocument() document.LoadXml("<?xml...
11
12299
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
7
2387
by: Andreas Hofmann | last post by:
Hello Folks! I've got a little problem here, which which really creeps me out at the moment. I've got some strings, which only contain numbers plus eventually one character as si-postfix (k for...
0
7125
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
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.