Javascript:将字符串转换为数字? [英] Javascript: Converting String to Number?

查看:126
本文介绍了Javascript:将字符串转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将变量从字符串转换为数字?

Is there any way to convert a variable from string to number?

例如,我有

var str = "1";

我可以将其更改为

var str = 1;


推荐答案

尝试 str = parseInt( str,10)

note :第二个参数表示用于解析的基数;在这种情况下,你的数字将被解析为十进制)

(note: the second parameter signifies the radix for parsing; in this case, your number will be parsed as a decimal)

然而,据说,类型这个术语在JavaScript中使用得非常松散。
你能解释为什么你关注变量的类型吗?

That being said, however, the term "type" is used quite loosely in JavaScript. Could you explain why are you concerned with variable's type at all?

这篇关于Javascript:将字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆