473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with SUM function

263 100+
Good afternoon to everyone.

I write this page htm with JavaScript code:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. <!--
  3.  
  4. function parseNumber(val)
  5. {
  6.   val = parseFloat(val);
  7.   if (isNaN(val)) return(0.0);
  8.  
  9.   return(val);
  10. }
  11.  
  12. var idList = [630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646];
  13.  
  14. function calcola(frmObj, id)
  15. {
  16.  
  17.   var sum1 = 0.0, sum2 = 0.0;
  18.  
  19.   for ( var n = 0 ; n < idList.length ; n++ )
  20.     {
  21.       sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
  22.       sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
  23.     }
  24.  
  25.   frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
  26.   frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
  27. }
  28.  
  29. // -->
  30. </script>        
  31.  
  32.       <td><input type="text" name="BANCO-630" size="5" value="33,18" onkeyup="calcola(this.form, 630)"></td>    
  33.       <td><input type="text" name="TAVOLI-630" size="5" value="128,73" onkeyup="calcola(this.form, 630)"></td> 
  34.       <td><input type="text" name="TOT_BANCO-630" size="5" value="902,63" readonly></td>
  35.       <td><input type="text" name="TOT_TAVOLI-630" size="5" value="1961,92" readonly></td>
  36.  
  37.       <td><input type="text" name="BANCO-631" size="5" value="0" onkeyup="calcola(this.form, 631)"></td>    
  38.       <td><input type="text" name="TAVOLI-631" size="5" value="6,5" onkeyup="calcola(this.form, 631)"></td> 
  39.       <td><input type="text" name="TOT_BANCO-631" size="5" value="0" readonly></td>
  40.       <td><input type="text" name="TOT_TAVOLI-631" size="5" value="0" readonly></td>
  41.  
  42.  
The script works in this way if updated quantity :

TOT_BANCO-630 = BANCO-630 +/- BANCO-631
TOT_TAVOLI-630 = TAVOLI-630 +/- TAVOLI-631

This script not return errors but the new sum are wrong.

Link of the page:
http://www.episunneo.net/venduto.htm

Help me please.
Viki
Mar 20 '08 #1
12 1503
viki1967
263 100+
Hi everyone.

I have problem whit this function:

Expand|Select|Wrap|Line Numbers
  1. function calcola(frmObj, id)
  2. {
  3.  
  4.   var sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
  5.  
  6.   for ( var n = 0 ; n < idList.length ; n++ )
  7.  
  8.     {
  9.  
  10.       sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
  11.       sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
  12.  
  13.       sum3 += parseNumber(frmObj["TOT_BANCO-" + idList[n]].value) + 
  14.               parseNumber(frmObj["TOT_TAVOLI-" + idList[n]].value);          
  15.  
  16.     }
  17.  
  18.   frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
  19.   frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
  20.   frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);
  21.  
  22.   var vl = frmObj["TOT_CON_IVA-" + idList[0]].value = sum3.toFixed(2) * 0.1 +
  23.                                                       parseNumber(frmObj["TOT_SENZA_IVA-" + idList[0]].value);
  24.  
  25.   frmObj["TOT_CON_IVA-" + idList[0]].value = vl.toFixed(3);                                                      
  26.  
  27.  
  28. }
  29.  
The link of the page web:
http://www12.asphost4free.com/viki1967/venduto.htm

Try change values line number 4:

BANCO = 3
TAVOLI = 3
TOTALE SENZA IVA = 1165.37 ???? its wrong

BANCO = 3.3
TAVOLI = 3.3
TOTALE SENZA IVA = 1155.40 !!!! its OK

Was going on ?

Can you help me?
Mar 21 '08 #2
acoder
16,027 Expert Mod 8TB
You're only setting the total for 630. Do you need to update for all totals?
Mar 22 '08 #3
acoder
16,027 Expert Mod 8TB
Since this is related somewhat to the first problem, I've merged the two threads.
Mar 22 '08 #4
viki1967
263 100+
You're only setting the total for 630. Do you need to update for all totals?

Yes, I don't have it any problem, thanks:

http://www12.asphost4free.com/viki1967/venduto.htm
Mar 22 '08 #5
acoder
16,027 Expert Mod 8TB
Does that mean it's working now?

If not, the figures that you mention don't match with the default values.
Mar 23 '08 #6
viki1967
263 100+
Can you give me a example please?
Mar 23 '08 #7
acoder
16,027 Expert Mod 8TB
I meant this:
Try change values line number 4:

BANCO = 3
TAVOLI = 3
TOTALE SENZA IVA = 1165.37 ???? its wrong

BANCO = 3.3
TAVOLI = 3.3
TOTALE SENZA IVA = 1155.40 !!!! its OK
The figures are something else, so I can't be sure what's right or what's not unless I manually sum the fields.
Mar 23 '08 #8
viki1967
263 100+
I meant this:

The figures are something else, so I can't be sure what's right or what's not unless I manually sum the fields.
Line 4:

BANCO = 3
TAVOLI = 0
TOTALE SENZA IVA = 2801.8

BANCO = 3.3 ( +0.3 )
TAVOLI = 3.3 ( +3.3 )
TOTALE SENZA IVA = 2805.4

2801.8 + 0.3 + 3.3 = 2805.4

It's OK ?
Mar 23 '08 #9
acoder
16,027 Expert Mod 8TB
That looks about right, so it's working. What changes did you make?
Mar 24 '08 #10
viki1967
263 100+
The lines 19 and 20 :

Expand|Select|Wrap|Line Numbers
  1. function calcola(frmObj, id)
  2. {
  3.  
  4.   var sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
  5.  
  6.   for ( var n = 0 ; n < idList.length ; n++ )
  7.  
  8.     {
  9.  
  10.       sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
  11.       sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
  12.  
  13.  
  14.  
  15.     }
  16.   frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
  17.   frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
  18.  
  19.   sum3 = sum1 + sum2;    
  20.   frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);
  21.  
  22.  
  23.   var vl = frmObj["TOT_CON_IVA-" + idList[0]].value = sum3.toFixed(2) * 0.1 +
  24.                                                       parseNumber(frmObj["TOT_SENZA_IVA-" + idList[0]].value);
  25.  
  26.   frmObj["TOT_CON_IVA-" + idList[0]].value = vl.toFixed(3);                                                      
  27.  
  28.  
  29. }
  30.  
Replace this:

Expand|Select|Wrap|Line Numbers
  1. sum3 += parseNumber(frmObj["TOT_BANCO-" + idList[n]].value) + 
  2.         parseNumber(frmObj["TOT_TAVOLI-" + idList[n]].value); 
  3.  
whit:

Expand|Select|Wrap|Line Numbers
  1.   sum3 = sum1 + sum2;    
  2. frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);
  3.  
Mar 24 '08 #11
acoder
16,027 Expert Mod 8TB
Thanks for posting and glad it's working!
Mar 24 '08 #12
viki1967
263 100+
You are welcome and I'am very happy to found this Community!
Mar 24 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
117
by: Peter Olcott | last post by:
www.halting-problem.com
1
by: Mohamed Fysal | last post by:
I have written a Regular DLL with many Export Functions and one CALLBACK fun ction . The callback function declared in the .cpp file of the Regular DLL is as fol lows: typedef BOOL...
4
by: Andy_Khosravi | last post by:
Hello, I'm having a problem with the MID function within Access 97. I have been trying to build a function to check to make sure that a field on a form does not have any spaces or dashes. This...
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
5
by: jbenner | last post by:
I have opened a PMR for this with IBM, and am not asking for advice from the DB2 DBA community. I am posting this as an FYI that DB2 Health Monitor, even at the latest version of DB2, still can cause...
2
by: Ravikiranreddy | last post by:
Hi every one, Am pasting the error code returned in log please help me in fixing it--- ERROR:An error occurred during the execution of the command "/opt/IBM/db2/V8.1/instance/dascrt -u...
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.