PDA

View Full Version : Add Total Of column of Numbers


Hawk
August 9th, 2011, 01:10 PM
I have have an external data file that I am using to display a list of courses. Each record will display the courses that match a field in my data. That part works fine.

One of the columns is a number that represents hours. I need to add all of the hours for a given record to get total hours and display that number.

Thanks,
Larry

step
August 9th, 2011, 01:45 PM
How about something like this to push the number of records from each record in the external data file into an array and then return the sum amount of that array?


var hours = [];
var totalHours = 0;
var records = externalDataFile.recordCount;

// Start an array for your hours
for (var i=1; i <= records; i++)
{
hours.push(externalDataFile.GetFieldValue(i,'Hours '));

}

//Sum your hours
for (var n=0; n< hours.length; n++){
totalHours += Int(hours[n]);
}

return totalHours;

Hawk
August 10th, 2011, 05:53 AM
Thanks for the help.

The only change was. totalHours += Int(hours[n]); To totalHours += StringToNumber(hours[n]);

I need to return a floation-point Number.

Hawk
August 10th, 2011, 06:38 AM
I'm having a problem with the math on this. It seems that if I have more than one record match in the external data file it doubles the returned value. If only one match is found it is correct.


//Check for a valid External File
if(FusionPro.Composition.isPreview == true || FusionPro.inValidation == true)
{
Rule("OnJobStart");
}
//Trim leading zeros from licenseid to find matches in External File
function trimNumber(s) {
while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
return s;
}
var s = Field("licenseid");
var hours = [];
var totalHours = 0;
var numRecsExtDF = XDF.recordCount;
//count records in database
for (var i=1; i <= numRecsExtDF; i++){
//count records that match licenseid, push to aray and return total hours.
if (XDF.GetFieldValue(i, 1) == trimNumber(s) && XDF.GetFieldValue(i, 5) == "Technical")
{
{
hours.push(XDF.GetFieldValue(i, 4));
}

for (var n=0; n< hours.length; n++){
totalHours += StringToNumber(hours[n]);
}
}

}
var result = totalHours.toFixed(2);
return result;


Can anyone tell me what I'm missing?

Dan Korn
August 10th, 2011, 08:57 AM
Why are you putting the values into an array and then adding them? Just add the numbers as you find them in the file:
if(FusionPro.inValidation)
Rule("OnJobStart");

var s = parseInt(Field("licenseid"), 10);
var totalHours = 0;
for (var i = 1; i <= XDF.recordCount; i++)
{
//count records that match licenseid and return total hours.
if (XDF.GetFieldValue(i, 1) == s && XDF.GetFieldValue(i, 5) == "Technical")
totalHours += StringToNumber(XDF.GetFieldValue(i, 4));
}
return totalHours.toFixed(2);
I simplified a few other things too.

Hawk
August 10th, 2011, 11:18 AM
That did it thanks for the help.