Solved

javascript - how to modify for decimal numbers?

Posted on 2014-10-16
9
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need help with share buttons 11 74
How Close unsubmited attempts 10 49
Record locking on classic ASP 3 41
Building JSON Results Table FROM DB 9 39
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

696 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