Error: Type ‘string’ is not assignable to type ‘number’ with toFixed()

I have simple function of calculating the sum of numbers and strings in columns in a table. The sum works well and gives me results. The issue is that the sum has a lot of decimal places and I want to convert it to 2 decimal places, whenever I try   

return sum.toFixed(2);

I end up with an error.

 Type 'string' is not assignable to type 'number'.

Example of the sum from some columns

615.7142868041998, 0,10, 34.0476188659667, 14.761905193328857

What I have so far. The table columns contains both integers and strings, am using the reducer function to get only numbers

getSum(columnNumber) {
    let sum = 0;
    const columnNumberToPropertyMap = [
    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);

How can I convert the sum to 2 decimal places. I have tried to pipe the result in the html | number : '1.2-2' it is not working also . thank you in advance

Read more here:

Content Attribution

This content was originally published by arriff at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: