var a = (123.456).toString(36)//"3f.gez4w97ry0a18ymf6qadcxr"
现在,如何使用该字符串恢复到原始数字?
注意:parseInt(number,36)
仅适用于整数。
最佳答案
您可以尝试使用 parseInt
分别解析整数和 float 部分,因为 parseFloat
不支持基数:
function parseFloatInBase(n, radix) {
var nums = n.split(".")
// get the part before the decimal point
var iPart = parseInt(nums[0], radix)
// get the part after the decimal point
var fPart = parseInt(nums[1], radix) / Math.pow(radix, nums[1].length)
return iPart + fPart
}
// this will log 123.456:
console.log(parseFloatInBase("3f.gez4w97ry0a18ymf6qadcxr", 36))
我除以radix ^ numLength
,因为我基本上是在numLength
空格上移动小数点。您可以像在数学课上一样执行此操作,因为您知道除以 10 会将小数移动一个空格,因为大多数数学都是以 10 为基数。示例:
123456 / 10 / 10 / 10 = 123.456
这相当于
123456 / (10 * 10 * 10) = 123.456
因此
123456 / (10 ^ 3) = 123.456
关于javascript - 与 toString(36) 相反?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985984/