javascript - typescript - 将字符串转换为数字

标签 javascript typescript casting

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

我的 totalbalancetemp 返回未定义,而 this.balance 等于 34,this.pastAmount 等于 23。

我在 Controller 中有这个,并使用 html 中的 exp 显示 totalbalancetemp

最佳答案

提供正确的类型。

let totalbalancetemp:number = balance + pastAmount

这将引发错误,因为您现在保证 totalbalancetemp 将是一个数字

The type String is not assignable to type 'number'

尝试以下操作:

let balance:string = '34',
    pastAmount:string = '23',
    totalbalancetemp:number = 0

totalbalancetemp = Number(balance) + Number(pastAmount)

alert(totalbalancetemp)

关于javascript - typescript - 将字符串转换为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666343/

相关文章:

javascript - 按给定的单元格值查找表行并删除该行

javascript - 将对象存储在 firebase 中以便稍后以简单的方式重新排序的正确方法是什么?

javascript - best_in_place nil 和 edit 函数不能在 rails4 上使用 bootstrap 应用程序

javascript - 使用 TypeScript AMD 导入 OSC 库无法正确构建 osc 对象

c - 将 int arg 转换为 double 的 Gcc 编译器选项?

javascript - 在点击事件上重置 jQuery Raty 选择 (jQuery)

javascript - 从共享消息服务触发事件以更改组件的内容

Angular react 形式正在被提交而无需提交

c# - 无法将 'System.Int16' 类型的对象转换为 'System.String' 类型

c - 类型类型转换节点结构以打印内容