【问题标题】:JavaScript - opposite of toString()?JavaScript - 与 toString() 相反?
【发布时间】:2011-01-08 04:55:59
【问题描述】:

当与数字一起使用时,toString() 方法使用可选基数指定的基本编号系统将数字转换为字符串表示形式。

例如数字 8 和 toString(2) 将返回“1000”。

有没有实现相反的方法?即将字符串“1000”转换回数字8?

【问题讨论】:

    标签: javascript tostring


    【解决方案1】:

    parseInt() 将基数作为其第二个参数,因此您可以使用parseInt("1000", 2) 来得到您想要的。

    【讨论】:

    • @AlexStack Number.prototype.toString(radix)
    【解决方案2】:

    您可以通过 raidx 2 使用 parseInt。 即

    parseInt("1000", 2) will return 8
    

    【讨论】:

      【解决方案3】:

      检查 parseInt() Javascript 函数: http://www.w3schools.com/jsref/jsref_parseInt.asp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2018-07-13
        • 2010-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多