In my code, I have a simple function that calculates the sum of numbers and strings in columns within a table. The sum calculation itself works fine and provides accurate results. However, the problem arises when I attempt to divide the total sum of each column by 100, as it gives me incorrect values.
Here is an example scenario:
I have a sum of 969.35
in column A. When I try to divide this value by 100
, I expect to get 9.69
. Instead, I am getting 0.51
. This discrepancy has perplexed me as to why this calculation is going wrong.
This is how I'm currently implementing the sum calculation:
getSum(columnNumber) {
let sum = 0;
const columnNumberToPropertyMap = [
"id",
"teamNumber",
"rural",
"completed",
"entirehouseholdabsent",
"postponed",
"refused",
"vacant",
"dwelling"
];
const property = columnNumberToPropertyMap[columnNumber];
return this.rural.reduce((acc, curr) => {
//const adder = Number(curr[property]) || 0;
const adder = isNaN(Number(curr[property])) ? 0 : Number(curr[property]);
sum = acc + adder
return sum.toFixed(2);
}, 0);
}
The above code successfully calculates the sum for each column.
Now, here's how I'm trying to calculate the percentage:
getPercentage(columnNumber) {
let sum = 0;
let percentage = 0;
const columnNumberToPropertyMap = [
"id",
"teamNumber",
"rural",
"completed",
"entirehouseholdabsent",
"postponed",
"refused",
"vacant",
"dwelling"
];
const property = columnNumberToPropertyMap[columnNumber];
return this.rural.reduce((acc, curr) => {
//const adder = Number(curr[property]) || 0;
const adder = isNaN(Number(curr[property])) ? 0 : Number(curr[property]);
sum = acc + adder
percentage = (sum)/100;
return percentage.toFixed(2);
}, 0);
}
If anyone can point out what mistake I might be making or suggest a solution, it would greatly help me understand and resolve this issue.