简体   繁体   中英

typescript - convert string to number

totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

My totalbalancetemp is returning undefined whereas this.balance is equal to 34 and this.pastAmount is equal to 23.

I have this in controller and displaying totalbalancetemp using exp in html

Supply the proper type.

let totalbalancetemp:number = balance + pastAmount

This will throw an error, because you are now guaranteeing that totalbalancetemp will be a number .

The type String is not assignable to type 'number'

Try the following:

 let balance:string = '34', pastAmount:string = '23', totalbalancetemp:number = 0 totalbalancetemp = Number(balance) + Number(pastAmount) alert(totalbalancetemp)

totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

请试试这个它应该可以工作

totalbalancetemp:number = (+this.balance) + (+this.pastAmount);
var totalbalancetemp = null; this.balance = 34; this.pastAmount = 23; 

totalbalancetemp = (Number(this.balance)) + (Number(this.pastAmount));

alert(totalbalancetemp);

-->totalbalancetemp - Define Variable (or) any type

如果 totalbalancetemp 是 angular 2 组件的一部分,则应由 this.totalbalancetemp 替换

在 .ts 文件中执行+this.balance或在模板文件中执行this.balance*1this.balance/1 on。

In typescript, we can convert the string in different ways. Here's the below methods for converting the string to number

x = parseInt('34')

OR

x = Number('34')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM