I have a variable set like this:
public Total: number=0;
Which is a cummulative variable. At ngOnInit, I have several places where I change that variable content, such as
this.Total=this.Total + MEDICALJSON[0].amount
this.Total=this.Total + HOSPITALJSON[0].amount
etc.
I round Total to two decimals, which Im doing this way:
this.Total=parseFloat(this.Total).toFixed(2);
This is working perfect at runtime and not throwing any error, but when I ng serve, I get this error on the terminal:
Error: src/app/quoteresult/quoteresult.component.ts:190:5 - error TS2322: Type 'string' is not assignable to type 'number'.
190 this.Total=parseFloat(this.Total).toFixed(2);
How can I get rid of the error when compiling? Thanks.
toFixed
returns a string formatted to the number of decimals desired, but Total
must be a number. Call Number
on the string to turn it back into a number:
this.Total = Number(this.Total.toFixed(2));
There isn't any need to call parseFloat
on it first, because it should start as a number.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments