Solved

javascript - how to modify for decimal numbers?

Posted on 2014-10-16
9
127 Views
Last Modified: 2015-01-02
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

0
Comment
Question by:fedkris
  • 3
  • 2
  • 2
9 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 332 total points
ID: 40384184
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
 

Author Comment

by:fedkris
ID: 40384204
hi, thx, it works, with decimal nbs with .   example: 5 + 6.1

what with decimal nbrs wit , ???    example 5 + 6,1 ?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 332 total points
ID: 40384349
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:fedkris
ID: 40386150
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 40386868
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
 

Author Comment

by:fedkris
ID: 40391454
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
 
LVL 33

Accepted Solution

by:
Big Monty earned 168 total points
ID: 40392403
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question