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 couldconsole.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
1 Answer
Reset to default 6try below to force change element.amount to number
this.listOfPeriods[index].declarations.forEach(element => {
summary = summary + (+element.amount);
});
本文标签: javascriptTypescript array sum instead concatenationStack Overflow
版权声明:本文标题:javascript - Typescript array sum instead concatenation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465079a2659497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论