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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Question about arithmetic operators

    I would like to get the sum of three values which I collect from myForm. Unfortunately my code computes them as strings and not integers. How can I compute integer values?


    Code:
    var LeftcolumnWidth = myForm.leftcolumn_width.value; // 160
    var MiddlecolumnWidth = myForm.middlecolumn_width.value; // 400
    var RightcolumnWidth = myForm.rightcolumn_width.value; // 140
    
    var MaintableWidth = LeftcolumnWidth + MiddlecolumnWidth + RightcolumnWidth; // should output 700 but
    alert('Main table is '+MaintableWidth+'pixels wide'); // outputs 160400140

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,480
    Thanks
    0
    Thanked 635 Times in 625 Posts
    See http://javascript.about.com/library/blstrnum.htm for information on seven different ways that JavaScript provides for converting text to numbers and the pros and cons of each.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks a lot. I've solved the problem by using

    Code:
    var MaintableWidth = Number(LeftcolumnWidth) + Number(MiddlecolumnWidth) + Number(RightcolumnWidth);
    Hope it helps someone.


  •  

    Posting Permissions

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