javascript - how to modify for decimal numbers?

hello,

i have a javascript, when people fill in numbers in fields, it makes a sum.

with whole (normal numbers) there's no problem, but with decimal numbers, the result is n/a

how can i modify this script, and calculate also decimal numbers?

<script type="text/javascript"><!--
function updatesum1() {
document.form.sum1.value = (document.form.a1.value - 0 ) + (document.form.a2.value - 0) + (document.form.a3.value -0 ) + (document.form.a4.value -0)  + (document.form.a5.value -0) + (document.form.a6.value -0) + (document.form.a7.value -0)  + (document.form.a8.value -0) + (document.form.a9.value -0) + (document.form.a10.value -0) + (document.form.a11.value -0);
}
//--></script>


i have multiple input boxes:

<td><input type=text size=15 maxlength=150 name="a1" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a2" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a3" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a4" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a5" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a6" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a7" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a8" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a9" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a10" onChange="updatesum1()"></td>
<td><input type=text size=15 maxlength=150 name="a11" onChange="updatesum1()"></td>



<td><input name="sum1" readonly style="border:0px; font-weight: bold;" ></td>

Open in new window

fedkrisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
function updatesum1() {
var a1 = document.form.a1.value * 1 || 0;
var a2 = document.form.a2.value * 1 || 0;
var a3 = document.form.a3.value * 1 || 0;
var a4 = document.form.a4.value * 1 || 0;
var a5 = document.form.a5.value * 1 || 0;
var a6 = document.form.a6.value * 1 || 0;
var a7 = document.form.a7.value * 1 || 0;
var a8 = document.form.a8.value * 1 || 0;
var a9 = document.form.a9.value * 1 || 0;
var a10 = document.form.a10.value * 1 || 0;
var a11 = document.form.a11.value * 1 || 0;
document.form.sum1.value =a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 +a 10 + a11;
}

Open in new window

0
fedkrisAuthor Commented:
hi, thx, it works, with decimal nbs with .   example: 5 + 6.1

what with decimal nbrs wit , ???    example 5 + 6,1 ?
0
leakim971PluritechnicianCommented:
6,1 is not a number in the javascript world.
now, you now that, ou can replace "," by "."
"6,1" is a string
"6.1" is a string
"6,1" * 1 i not a number
"6.1" * 1 is a number

Want to replace "," by "." ?
example : document.form.a1.value.replace(",", ".")
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

fedkrisAuthor Commented:
ok thx,

last question, i wanna make a loop

exemple

my fields in the form are a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11
and then b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11
it goes to h1, ....


i don't want to repeat the script that many times, is there an easy solution? a loop?
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you can do something like this as a loop:

function updatesum1() {
    var totalSum = 0;
    var fieldPrefixes = ['a', 'b', 'c', 'd'. 'e', 'f', 'g', 'h'];

    for each ( var fld in fieldPrefixes ) 
         for ( var fldNum = 1; fldNum <= 11; fldNum++ ) 
              totalSum += window.document.forms['frmName'].elements[ fld + fldNum ].value * 1 || 0;

    document.form.sum1.value = totalSum;
}

Open in new window


just change the frmName text to the name of your form. Also, keep in mind this'll add up ALL of the fields, not just a subset of them. if you want to all of the "A" boxes in one variable, and all of the "B" boxes in a separate variable, that's easy enough to do, just let us know
0
fedkrisAuthor Commented:
hi, thx for the answer, in fact, we need a separate variable, all the a boxes, count together, all the b-boxes,....

can you show me how to? thx
0
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
you can modify it to look like this:

function updatesum1() {
    var totalSum = 0;
    var fieldPrefixes = ['a', 'b', 'c', 'd'. 'e', 'f', 'g', 'h'];

    for each ( var fld in fieldPrefixes ) {
         for ( var fldNum = 1; fldNum <= 11; fldNum++ ) 
              totalSum += window.document.forms['frmName'].elements[ fld + fldNum ].value * 1 || 0;

         window.document.forms['frmName'].elements[ 'sum_' + fld ].value
         totalSum = 0;
    }
}

Open in new window


then add the following hidden fields to your form:

<input type="hidden" name="sum_a" id="sum_a" value="" />
<input type="hidden" name="sum_b" id="sum_b" value="" />
<input type="hidden" name="sum_c" id="sum_c" value="" />
<input type="hidden" name="sum_d" id="sum_d" value="" />
<input type="hidden" name="sum_e" id="sum_e" value="" />
<input type="hidden" name="sum_f" id="sum_f" value="" />
<input type="hidden" name="sum_g" id="sum_g" value="" />
<input type="hidden" name="sum_h" id="sum_h" value="" />

Open in new window


this'll add up each sections text boxes (a-h) and place them in the hidden fields. you could also just create local variables in the function instead of the hidden fields, it all depends on what you need to do with those values.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.