...

View Full Version : Question about arithmetic operators



guvenck
07-05-2007, 10:35 PM
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?




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

felgall
07-05-2007, 11:57 PM
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.

guvenck
07-06-2007, 07:42 PM
Thanks a lot. I've solved the problem by using



var MaintableWidth = Number(LeftcolumnWidth) + Number(MiddlecolumnWidth) + Number(RightcolumnWidth);


Hope it helps someone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum