Solved

javascript - how to modify for decimal numbers?

Posted on 2014-10-16
9
120 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:fedkris
Comment Utility
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 32

Expert Comment

by:Big Monty
Comment Utility
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
Comment Utility
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 32

Accepted Solution

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now