Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Adding simple maths to javascript value

    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 :

    Code:
    <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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    first you need to set a variable with that value
    Code:
    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:
    Code:
    var myVar= Number(xmlDoc.documentElement.getElementsByTagName("data1")[0].childNodes[0].nodeValue);
    Now do whichever math operation you need and write the final result:
    Code:
    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.
    Last edited by Kor; 10-24-2006 at 11:13 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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 ..
    Code:
    var total = (myVar/1000)*3;
    Cheers again
    Sir P

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    As an update.. I have tried ...
    Code:
    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

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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

    Code:
    var total = ((myVar+200)*5).toFixed(3);
    document.getElementById("data1").innerHTML=total;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •