Angular 2 - Getting Type 'string' is not assignable to type 'number'

Danielle

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.

CertainPerformance

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.

edited at
0

Comments

0 comments
Login to comment

Related

Argument of type 'number' is not assignable to parameter of type 'string' in angular2.

Getting Error: Argument of type 'number' is not assignable to parameter of type 'string'

Type number is not assignable to type string

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

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

Angular Type 'string' is not assignable to type 'Number' ...but it never is a string

Argument of type 'number' is not assignable to parameter of type 'string' - Typescript and Angular

Type 'number' is not assignable to type '[string | number, string]'

angular2 Type 'Subject<{}>' is not assignable to type '() => Subject<number>'

Angular2 Error: Type 'number' is not assignable to type 'NumberConstructor'

Type 'string | number | symbol' is not assignable to type 'string'

Type 'number' is not assignable to type 'Date' in Angular

Type String is not assignable to 'never' Angular

argument of type 'number' is not assignable to a parameter of type 'string'

Type 'string | number' is not assignable to type 'never'

Typescript Type 'number' is not assignable │ to type 'string'

Argument of type 'number' is not assignable to parameter of type 'string'

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

TypeScript error: Type 'string' is not assignable to type 'number | ""'

Type 'number' is not assignable to type 'string'. ts(2322)

Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'

Angular error Type string is not assignable to type never

Angular 7.2.0: Type 'string' is not assignable to type 'RunGuardsAndResolvers'

Type 'null' is not assignable to type 'string'. Angular/TypeScript

Angular 12 - is not assignable to type '[number, number]

Argument of type 'string[]' is not assignable to parameter of type 'number[]'. Type 'string' is not assignable to type 'number'

Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string

Angular, typescript, google charts - Type (Date|number)[][] is not assignable to type Array<Array<string|number>>

Type \'Observable<{}>\' is not assignable to type in angular 2