Here's a quick hack of the script to test: const repeatingSum = (destination, section, fields, config = {}) => {
if (!Array.isArray(fields)) fields = [fields];
getSectionIDs(`repeating_${section}`, idArray => {
const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))],[]);
getAttrs(attrArray, v => {
console.log("===== values of v: "+ JSON.stringify(v) +" =====");
// getValue: if not a number, returns 1 if it is 'on' (checkbox), otherwise returns 0..
const getValue = (section, id,field) => parseFloat(v[`repeating_${section}_${id}_${field}`], 10) || (v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : 0);
let sumTotal = idArray.reduce((total, id) => total + fields.reduce((subtotal,field) => subtotal * getValue(section, id,field),1),0);
if(config && config.round)
sumTotal = Math.round(sumTotal * Math.pow(10,config.round ||0 ) ) / Math.pow(10,config.round ||0 );
else if(config && config.floor)
sumTotal = Math.floor(sumTotal * Math.pow(10,config.round ||0 ) ) / Math.pow(10,config.floor ||0 );
else if(config && config.ceil)
sumTotal = Math.ceil(sumTotal * Math.pow(10,config.ceil ||0 ) ) / Math.pow(10,config.ceil ||0 );
if(config && config.multiplier)
sumTotal = sumTotal * config.multiplier;
setAttrs({[destination]: sumTotal});
});
});
}; Add a fourth paremeter, enclosed in curly brackets, with the type of rounding, and the number of digits to round to: For instance, if you have a repeating_inventory, are multiplying quantity and weight together, and saving to a total_weight attribute, and wanted to round to two decimals, you'd use: repeatingSum('total_weight', 'inventory', ['quantity', 'weight'], {round: 2}); Give that a try.