JavaScript - 与 toString() 相反?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript - opposite of toString()?
提问by posfan12
When used with numbers, the toString() method converts a number into a string representation using the base numbering system specified by the optional radix.
当与数字一起使用时,toString() 方法使用由可选基数指定的基本编号系统将数字转换为字符串表示形式。
For instance the number 8 and toString(2) would return "1000".
例如数字 8 和 toString(2) 将返回“1000”。
Is there a method to achieve the opposite? I.e., convert the string "1000" back into the number 8?
有没有办法实现相反的目标?即,将字符串“1000”转换回数字 8?
回答by Jeremy Ruten
parseInt()takes a radix as its second argument, so you can do parseInt("1000", 2)to get what you want.
parseInt()将基数作为它的第二个参数,所以你可以做得到parseInt("1000", 2)你想要的。
回答by Chandu
You can use parseInt using a raidx 2. i.e
您可以使用 parseInt 使用 raidx 2。即
parseInt("1000", 2) will return 8
回答by turtlepick
Check the parseInt() Javascript function: http://www.w3schools.com/jsref/jsref_parseInt.asp
检查 parseInt() Javascript 函数:http: //www.w3schools.com/jsref/jsref_parseInt.asp

