Numeric value problems

S

Scott Sauyet

FF does some static optimizations @ compile time:

(function f () { 2+2; return ("3."+"14")*"2"+x;}).toSource()
-> "(function f() {return 6.28 + x;})"

Very cool!

-- Scott
 
D

Dr J R Stockton

In comp.lang.javascript message <13be6b2e-c975-4c4c-8dd3-fee54ed10b27@a6
g2000yqm.googlegroups.com>, Tue, 19 Jan 2010 12:31:41, chumley
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;
}

As far as I recall, all of the other answers have missed something.

The OP wrote "I have a function that simply totals the value from
previous calculations from textboxes".

The values should therefore be remembered as Numbers when calculated,
and do not need to be converted from form control values.



If it were a case of reading user input rather than machine results,
another step should be inserted.

The strings assigned to A, B, C, D need (generally) to be validated as
strings before conversion to Number. While there is rarely a way to be
sure that each digit is the right digit, one can check that the number
of digits is acceptable, that none of the "3" digits have been typed as
"e" (a fault sometimes made by keyboard-watchers, that "O" and "l" are
not present, etc.

Most applications in fact require between one and a few digits only, or
a string with a given number of decimal places. For currencies in most
of the world, anything other than zero or two decimal places should
usually be rejected as a probable error.

A simple RegExp test suffices in most cases.

That should be mentioned in FAQ 5.4, "1+1", with a link to a new part of
FAQ 7 "RegExps".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top