admin管理员组

文章数量:1426028

I have a problem with calculating a sum in typescript where I have 2 numbers that concatenate instead of summing it all up. I have looked up this issue and have seen several topics covering this problem where the solution usually was like this:

"use ParseInt() or ParseFloat() to convert your strings to integers"

The problem is that I don't have strings and even though that I use numbers they still concatenate.

My code is as follows:

     updateSummaryAmount(index: number){
        let summary = 0;

        this.listOfPeriods[index].declarations.forEach(element => {
            summary = summary + element.amount;
        });

        this.listOfPeriods[index].summary = summary;
    }

If I sum

0,55

And

0,45

I get

00,550,45

When I try to use parseInt() or parseFloat(0 I get the following typescript error:

[ts} Argument of type 'number'  is not assignable to parameter of type 'string'.

I have tried to sum with Math.floor(), just to test, and this works but obviously gives me floored down numbers that I don't want.

How do I sum up 2 values in my case?

I have a problem with calculating a sum in typescript where I have 2 numbers that concatenate instead of summing it all up. I have looked up this issue and have seen several topics covering this problem where the solution usually was like this:

"use ParseInt() or ParseFloat() to convert your strings to integers"

The problem is that I don't have strings and even though that I use numbers they still concatenate.

My code is as follows:

     updateSummaryAmount(index: number){
        let summary = 0;

        this.listOfPeriods[index].declarations.forEach(element => {
            summary = summary + element.amount;
        });

        this.listOfPeriods[index].summary = summary;
    }

If I sum

0,55

And

0,45

I get

00,550,45

When I try to use parseInt() or parseFloat(0 I get the following typescript error:

[ts} Argument of type 'number'  is not assignable to parameter of type 'string'.

I have tried to sum with Math.floor(), just to test, and this works but obviously gives me floored down numbers that I don't want.

How do I sum up 2 values in my case?

Share Improve this question edited Sep 27, 2017 at 8:03 Nemani 7846 silver badges12 bronze badges asked Sep 27, 2017 at 7:51 Niek JonkmanNiek Jonkman 1,0562 gold badges15 silver badges36 bronze badges 6
  • 1 what does console.log(typeof element.amount) give you if you put it inside the forEach loop? – simne7 Commented Sep 27, 2017 at 7:54
  • 1 Sorry to repeat the obvious, but if what your saying is true you're dealing with strings. Please provide a minimal reproducible example to demonstrate the problem, and include an example of what is in this.listOfPeriods[index].declarations. You could console.log it an include the result in your question – Jamiec Commented Sep 27, 2017 at 7:55
  • 1 and tell us what argument you give on parseFloat() (parseInt() is not suited here) – Pac0 Commented Sep 27, 2017 at 7:55
  • It's a little confusing, your amounts aren't strings, but can't be summed !!! – cнŝdk Commented Sep 27, 2017 at 8:04
  • the result of 0,45 + 0,55 should be 1,00 or 0,100 or just 1? – Ammar Commented Sep 27, 2017 at 8:08
 |  Show 1 more ment

1 Answer 1

Reset to default 6

try below to force change element.amount to number

this.listOfPeriods[index].declarations.forEach(element => {
  summary = summary + (+element.amount);
});

本文标签: javascriptTypescript array sum instead concatenationStack Overflow