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
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    js math problem?

    hi,
    I'm trying to add some numbers together using javascript,
    but instead of the numbers being added together, it's just putting the numbers side by side.
    example

    1 + 25 gives me 125.

    my code:

    function Add(){
    var tx=document.forms[0].tax;
    var ot=document.forms[0]. order_total
    var q=document.forms[0].qty;
    var c=document.forms[0].price_each;
    var t=document.forms[0].amount;

    if (isNaN(q.value)||isNaN(c.value)){
    q.value=q.value.replace(/\D/g,'');
    c.value=c.value.replace(/\D/g,'');
    tx.value=tx.value.replace(/\D/g,'');
    ot.value=ot.value.replace(/\D/g,'');
    t.value=ot.value.replace(/\D/g,'');
    alert('Only Numbers Allowed');
    }
    if (q.value.length>0&&c.value.length>0){
    t.value=parseInt(q.value) * parseInt(c.value);
    temp = t.value * tx.value / 100 + t.value + ot.value;
    ot.value = Math.round(temp*100)/100;
    }
    }

    any ideas what I'm doing wrong here?

    thanks in advance for your time!
    -Ken
    Last edited by angst; 12-20-2005 at 04:24 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    If either of the operands of the + operator is a sting, it will perform concatenation instead of addition. And I believe
    Code:
    temp = t.value * tx.value / 100 + t.value + ot.value;
    will be a string.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    umm,
    I'm not really sure what you mean,
    I'm pretty new to this stuff still, and can't you tell me how I can fix this problem? do they need to be cast as Int's? thats what I would do in ASP or PHP for this kind of problem..?

    thanks again,
    -ken

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok,
    it's adding the numbers together now,
    but still wrong,

    example:
    if I do 1 + 28.75
    I get 29, this is very strange, and I have no idea what I'm doing wrong here..

    if (q.value.length>0&&c.value.length>0){
    t.value=parseInt(q.value) * parseInt(c.value);
    temp = t.value * tx.value / 100 + t.value;
    ot.value = parseInt(temp) + parseInt(ot.value);
    //ot.value = Math.round(temp*100)/100;
    }

    any help would be greate!

    thanks in advance for your time!
    -Ken

  • #5
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    use parseFloat(string)... or Number(string)...

    parseInt() returns an integer

    parseInt('28.75') = 28
    parseFloat('28.75')=28.75
    Number('28.75')=28.75

    PS : value of an element is a string... it needs to be converted to integer or float according to the requirement...
    Last edited by Nischumacher; 12-20-2005 at 04:58 PM.
    - NS 666
    .net DEVILoper

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    kool!
    that fixes my problem:-)

    thanks!
    -ken


  •  

    Posting Permissions

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