...

View Full Version : Adding simple maths to javascript value



sir pannels
10-24-2006, 09:38 AM
morning everyone ..

I'm stuck on some javascript, I have been using the W3schools standard for XML parsing with Javascript using the microsoft xmldom object ...

I have the following XML parser :



<script type="text/javascript">
var xmlDoc;
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("Data.xml");
getdata();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("Data.xml");
xmlDoc.onload=getdata;
}
else
{
alert('Your browser cannot handle this script');
}
}

function getdata()
{
document.getElementById("data1").innerHTML=xmlDoc.getElementsByTagName("data1")[0].childNodes[0].nodeValue;

}
</script>


I will return the xml elements on the HTML by using this:


<span id="data1"></span>


So far this works great, no problems at all... The problem I do stumble across is that I need to do some mathmatical calculations on some of the elements, say .. 'data1'.. How do I do this?
Do I do it before the document.getElementById... or after? Or during? Its quite simple maths, just adding and divding..

JS not being my primary language this one is new on me, I'm quite interested to find the result.

If any one could help, that would be great.
Kind Regards,
P :D

Kor
10-24-2006, 11:10 AM
first you need to set a variable with that value


var myVar= xmlDoc.documentElement.getElementsByTagName("data1")[0].childNodes[0].nodeValue;

But you should consider that value is a string, not a decimal number, thus the code should have been:


var myVar= Number(xmlDoc.documentElement.getElementsByTagName("data1")[0].childNodes[0].nodeValue);


Now do whichever math operation you need and write the final result:


var total = (myVar+200)*5;
document.getElementById("data1").innerHTML=total


Note that, in case of add/substract, the transformation into a decimal number is essential (there are several methods, I prefere Number() method, as you saw). Otherwise, in case of an add opration, for instance, the result will be a concatenated string, not a math sum number.

sir pannels
10-24-2006, 11:41 AM
Ah I understand, thank you KOR..

Just simply declaring a var using the document.getElement..instead of printing it right away.. that is quite simple..

Thanks for your words on the string not being a decimal etc aswell, very helpful ...

Something I'am wondering though, is it possible to round to x decimal places in this calculation ..


var total = (myVar/1000)*3;


Cheers again
Sir P

sir pannels
10-24-2006, 12:36 PM
As an update.. I have tried ...


var total = (myVar+200)*5;
total = round_decimals(total, 3)
document.getElementById("data1").innerHTML=total

how it seems to totally chuck the calculation, can anyone see what I did wrong?

many thanks

Kor
10-24-2006, 12:51 PM
use toFixed() method. Important: note that toFixed() returns again a string. Keep that in mind if you want to have further math operations with the total variable



var total = ((myVar+200)*5).toFixed(3);
document.getElementById("data1").innerHTML=total;

sir pannels
10-25-2006, 12:29 PM
Hi Kor,

Ok thank you for your help. Much apperciated again.

I actually have the same issue now but for squaring a variable.. think im going to open new thread though as it does not concern the initial problem from this one

many thanks :)
p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum