toString(36) 的对面?

Opposite of toString(36)?(toString(36) 的对面?)
本文介绍了toString(36) 的对面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

var a = (123.456).toString(36)//"3f.gez4w97ry0a18ymf6qadcxr"

现在,如何使用该字符串恢复为原始数字?

Now, how do I revert back to the original number using that string?

注意:parseInt(number,36) 仅适用于整数.

Note: parseInt(number,36) only works for integers.

推荐答案

您可以尝试使用 parseInt 分别解析整数和浮点部分,因为 parseFloat 不支持基数:

You could try parsing the integer and float parts separately with parseInt, as parseFloat does not support a radix:

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 为底的.示例:

I am dividing by radix ^ numLength because I am basically moving the decimal point over numLength spaces. You would do this just like in math class, because as you know dividing by 10 moves the decimal over one space, because most math is in base 10. Example:

123456 / 10 / 10 / 10 = 123.456

这相当于

123456 / (10 * 10 * 10) = 123.456

因此

123456 / (10 ^ 3) = 123.456

这篇关于toString(36) 的对面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)