Velocity Reviews > Numeric value problems

Numeric value problems

chumley
Guest
Posts: n/a

 01-19-2010
I have a function that simply totals the value from previous
calculations from textboxes, but when trying to add the totals up
after assigning each textbox value a variable,the total in my
grandtotal box is just the values appended to each other (i.e . If
value A is 150 and value B is 200, the grandtotal box displays 150200:

function calculate2(oform, prefix) {
var A = document.oform.vn_stVis.value;
var B = document.oform.ja_stVis.value;
var C = document.oform.ta_stVis.value;
var D = document.oform.sa_stVis.value;
//var E = (A + B + C + D); -- this just appends the total literally to
each other in grandtotal box

var E = (A + B + C + D);
document.oform.gt_stVis.value = E;
}

??
chumley

Evertjan.
Guest
Posts: n/a

 01-19-2010
chumley wrote on 19 jan 2010 in comp.lang.javascript:

> I have a function that simply totals the value from previous
> calculations from textboxes, but when trying to add the totals up
> after assigning each textbox value a variable,the total in my
> grandtotal box is just the values appended to each other (i.e . If
> value A is 150 and value B is 200, the grandtotal box displays 150200:
>
> function calculate2(oform, prefix) {

What do you want to do with "prefix"?

It seems to me this is not yor code,
but that you are using code from someone else without understanding it.

> var A = document.oform.vn_stVis.value;
> var B = document.oform.ja_stVis.value;
> var C = document.oform.ta_stVis.value;
> var D = document.oform.sa_stVis.value;

These A B C and D will contain strings strings.

> //var E = (A + B + C + D); -- this just appends the total literally to
> each other in grandtotal box

Indeed.

Why the () they do not help anything.

> var E = (A + B + C + D);

You woyuld first have to change the strings to numeric values,
like:

A = +A;

If the string is not converteble to a numeric value,
you will get NaN:

var E = +A + +B + +C + +D;

> document.oform.gt_stVis.value = E;
>}

The long and strange names you give,
will deter you from understanding,
keep it simple, try this:

============== test.html ===================
<script type='text/javascript'>

function calc(f) {
f.e.value = +f.a.value + +f.b.value +
+f.c.value + +f.d.value;
return false; // to prevent submission.
};

</script>

<form onsubmit='return calc(this)'>
<br><input name='a'> a
<br><input name='b'> b
<br><input name='c'> c
<br><input name='d'> d
<br><br><input type='submit' value='calculate'>
</form>
===========================================

--
Evertjan.
The Netherlands.

Erwin Moller
Guest
Posts: n/a

 01-19-2010
chumley schreef:
> I have a function that simply totals the value from previous
> calculations from textboxes, but when trying to add the totals up
> after assigning each textbox value a variable,the total in my
> grandtotal box is just the values appended to each other (i.e . If
> value A is 150 and value B is 200, the grandtotal box displays 150200:
>
> function calculate2(oform, prefix) {
> var A = document.oform.vn_stVis.value;
> var B = document.oform.ja_stVis.value;
> var C = document.oform.ta_stVis.value;
> var D = document.oform.sa_stVis.value;
> //var E = (A + B + C + D); -- this just appends the total literally to
> each other in grandtotal box
>
> var E = (A + B + C + D);
> document.oform.gt_stVis.value = E;
> }
>
>
> ??
> chumley

Hi,

That happens because the values from your textfields are threated as
strings.
And by an unlucky coincedence JavaScript uses the + sign for string

Solution: Simply cast the strings to numbers using:
parseInt() for integers
or parseFloat() for floating point numbers

Warning: If you use parseInt be sure to supply the radix (propably 10).

In short:
var num1 = "23";
var num2 = "11";

alert (parseInt(num1,10) + parseInt(num2,10)); // 34

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

Dmitry A. Soshnikov
Guest
Posts: n/a

 01-19-2010
On Jan 19, 11:31*pm, chumley <(E-Mail Removed)> wrote:
> I have a function that simply totals the value from previous
> calculations from textboxes, but when trying to add the totals up
> after assigning each textbox value a variable,the total in my
> grandtotal box is just the values appended to each other (i.e . If
> value A is 150 and value B is 200, the grandtotal box displays 150200:
>
> function calculate2(oform, prefix) {
> var A = document.oform.vn_stVis.value;
> var B = document.oform.ja_stVis.value;
> var C = document.oform.ta_stVis.value;
> var D = document.oform.sa_stVis.value;
> //var E = (A + B + C + D); -- this just appends the total literally to
> each other in grandtotal box
>
> var E = (A + B + C + D);
> document.oform.gt_stVis.value = E;
>
> }
>
> ??

That because with string (and values from textboxes are strings) "+"
is used as concatenation. To use "+" for the numeric mathematical
meaning, use manual type conversion to numbers (e.g. function parseInt
(...) or unary "+" operator):

var
form = document.forms['oform'],
a = form.elements['vn_stVis'].value, // string
b = form.elements['ja_stVis'].value; // also string

alert(parseInt(a, 10) + parseInt(b, 10)); // number
alert(+a + +b); // also number, faster than parseInt, but in this case

/ds

RobG
Guest
Posts: n/a

 01-19-2010

chumley wrote:
> I have a function that simply totals the value from previous
> calculations from textboxes, but when trying to add the totals up
> after assigning each textbox value a variable,the total in my
> grandtotal box is just the values appended to each other (i.e . If
> value A is 150 and value B is 200, the grandtotal box displays 150200:

You've already got a number of answers, but just to round out the
discussion the answer is in the FAQ:

5.4 Why does 1+1 equal 11? or How do I convert a string to a number?
<URL: http://www.jibbering.com/faq/#typeConvert >

--
Rob

chumley
Guest
Posts: n/a

 01-20-2010
thanks for responses. i was able to add a little math to force the
variable to be numeric:

var A = document.oform.vn_stVis.value * 1;
var B = document.oform.ja_stVis.value * 1;
.....etc....

On Jan 19, 12:31*pm, chumley <(E-Mail Removed)> wrote:
> I have a function that simply totals the value from previous
> calculations from textboxes, but when trying to add the totals up
> after assigning each textbox value a variable,the total in my
> grandtotal box is just the values appended to each other (i.e . If
> value A is 150 and value B is 200, the grandtotal box displays 150200:
>
> function calculate2(oform, prefix) {
> var A = document.oform.vn_stVis.value;
> var B = document.oform.ja_stVis.value;
> var C = document.oform.ta_stVis.value;
> var D = document.oform.sa_stVis.value;

> //var E = (A + B + C + D); -- this just appends the total literally to
> each other in grandtotal box
>
> var E = (A + B + C + D);
> document.oform.gt_stVis.value = E;
>
> }
>
> ??
> chumley

Scott Sauyet
Guest
Posts: n/a

 01-20-2010
On Jan 19, 8:00*pm, chumley <(E-Mail Removed)> wrote:
> thanks for responses. i was able to add a little math to force the
> variable to be numeric:
>
> var A = document.oform.vn_stVis.value * 1;
> var B = document.oform.ja_stVis.value * 1;
> ....etc....

Although that works, you should probably take the advice you came here
seeking.

"parseInt"/"parseFloat" are the most explicit way to do the
conversion, the unitary "+" is probably the most efficient. Either is
more recognizable and hence more easily maintainable than yours.

"+myVar" does a numeric conversion only.

"myVar * 1" does a numeric conversion followed by a multiplication.

Cheers,

-- Scott

Lasse Reichstein Nielsen
Guest
Posts: n/a

 01-20-2010
Scott Sauyet <(E-Mail Removed)> writes:

> "+myVar" does a numeric conversion only.
>
> "myVar * 1" does a numeric conversion followed by a multiplication.

An optimizing implementation might recognize the futility of
multiplying by one and just do the numeric conversion

Personally I prefer using "Number(myVar)" for readability, even though
the prefix plus is usually slightly faster (it doesn't need to look up
"Number" in the global object and call the function). Speed is only
important if it's not fast enough.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

Scott Sauyet
Guest
Posts: n/a

 01-20-2010
On Jan 20, 4:12*pm, Lasse Reichstein Nielsen <(E-Mail Removed)>
wrote:
> Scott Sauyet <(E-Mail Removed)> writes:
> > "+myVar" does a numeric conversion only.

>
> > "myVar * 1" does a numeric conversion followed by a multiplication.

>
> An optimizing implementation might recognize the futility of
> multiplying by one and just do the numeric conversion

Have you ever seen an optimizer that smart, though?

> Personally I prefer using "Number(myVar)" for readability, even though
> the prefix plus is usually slightly faster (it doesn't need to look up
> "Number" in the global object and call the function). Speed is only
> important if it's not fast enough.

Yes, there is much to be said for that version too. It's at least as
explicit as parseInt, and it doesn't risk getting lost in middle of
punctuation noise.

-- Scott

David Mark
Guest
Posts: n/a

 01-20-2010
On Jan 20, 4:55 pm, Scott Sauyet <(E-Mail Removed)> wrote:
> On Jan 20, 4:12 pm, Lasse Reichstein Nielsen <(E-Mail Removed)>
> wrote:
>
> > Scott Sauyet <(E-Mail Removed)> writes:
> > > "+myVar" does a numeric conversion only.

>
> > > "myVar * 1" does a numeric conversion followed by a multiplication.

>
> > An optimizing implementation might recognize the futility of
> > multiplying by one and just do the numeric conversion

>
> Have you ever seen an optimizer that smart, though?
>
> > Personally I prefer using "Number(myVar)" for readability, even though
> > the prefix plus is usually slightly faster (it doesn't need to look up
> > "Number" in the global object and call the function). Speed is only
> > important if it's not fast enough.

>
> Yes, there is much to be said for that version too. It's at least as
> explicit as parseInt, and it doesn't risk getting lost in middle of
> punctuation noise.
>

As for parseInt vs. Number (or +), it depends on what you allow for
the input. Do you want "100px" to translate to 100 or is it an error?