JS中的数字系统转换(输入字母时错误)

发布于 2025-02-03 19:42:49 字数 3165 浏览 5 评论 0原文

我有一个转换器(数字系统)。一切都很好,但是当我试图将十二指肠(12)转换为十进制(10)时,就会出现问题。 当我在DuodeCimals中键入“ A”时,我会在小数点的位置出现错误 - 输出应为10,但它是NAN(基本上是当我键入字母而不是数字时(也是十六进制)时,有一个错误)。

    /* Calculation Board */

const config = {
    "binary": {
        "binary": v => v,
        "octal": v => parseInt(v, 2).toString(8),
        "decimal": v => parseInt(v, 2).toString(10),
        "duodecimal": v => parseInt(v, 2).toString(12),
        "hexadecimal": v => parseInt(v, 2).toString(16),
        "vigesimal": v => parseInt(v, 2).toString(20),
        "tetravigesimal": v => parseInt(v, 2).toString(24),
        "duotrigesimal": v => parseInt(v, 2).toString(32),
        "hexatrigesimal": v => parseInt(v, 2).toString(36),
    },
    "octal": {
        "binary": v => parseInt(v, 8).toString(2),
        "octal": v => v,
        "decimal": v => parseInt(v, 8).toString(10),
        "duodecimal": v => parseInt(v, 8).toString(12),
        "hexadecimal": v => parseInt(v, 8).toString(16),
        "vigesimal": v => parseInt(v, 8).toString(20),
        "tetravigesimal": v => parseInt(v, 8).toString(24),
        "duotrigesimal": v => parseInt(v, 8).toString(32),
        "hexatrigesimal": v => parseInt(v, 8).toString(36),
    },
    "decimal": {
        "binary": v => v.toString(2),
        "octal": v => v.toString(2),
        "decimal": v => v,
        "duodecimal": v => v.toString(12),
        "hexadecimal": v => v.toString(16),
        "vigesimal": v => v.toString(20),
        "tetravigesimal": v => v.toString(24),
        "duotrigesimal": v => v.toString(32),
        "hexatrigesimal": v => v.toString(36),
    },
    "duodecimal": {
        "binary": v => parseInt(v, 12).toString(2),
        "octal": v => parseInt(v, 12).toString(8),
        "decimal": v => parseInt(v, 12),
        "duodecimal": v => v,
        "hexadecimal": v => parseInt(v, 12).toString(16),
        "vigesimal": v => parseInt(v, 12).toString(20),
        "tetravigesimal": v => parseInt(v, 12).toString(24),
        "duotrigesimal": v => parseInt(v, 12).toString(32),
        "hexatrigesimal": v => parseInt(v, 12).toString(36),
    },
    
}

    /* Calculate sum */

const listFrom = document.getElementById("listFrom");
const listTo = document.getElementById("listTo");

inputPlace.addEventListener("keyup", function calculateInput(){
    calculate();
});

listFrom.addEventListener("change", function calculateFrom(){
    calculate();
});

listTo.addEventListener("change", function calculateTo(){
    calculate();
});

function calculate() {
    const listFromV = document.getElementById("listFrom").value;
    const listToV = document.getElementById("listTo").value;
    const inputPlace = parseFloat(document.getElementById("inputPlace").value);
    
    const fn = config[listFromV][listToV];
    document.getElementById("resultPlace").innerHTML = fn(inputPlace);
    
    if (document.getElementById("inputPlace").value == "" || document.getElementById("inputPlace").value == 0) {
        document.getElementById("resultPlace").innerHTML = "0.00";
    }
};

有办法解决这个问题吗?

I have a converter (numeral system). Everything works fine, but when I am trying to convert duodecimal (12) to decimal (10) there is a problem.
When I type "a" in duodecimals I have an error in the place of decimals - the output should be 10, but it's NaN (basically when I type a letter instead of a number (in hex too) there is this error).

    /* Calculation Board */

const config = {
    "binary": {
        "binary": v => v,
        "octal": v => parseInt(v, 2).toString(8),
        "decimal": v => parseInt(v, 2).toString(10),
        "duodecimal": v => parseInt(v, 2).toString(12),
        "hexadecimal": v => parseInt(v, 2).toString(16),
        "vigesimal": v => parseInt(v, 2).toString(20),
        "tetravigesimal": v => parseInt(v, 2).toString(24),
        "duotrigesimal": v => parseInt(v, 2).toString(32),
        "hexatrigesimal": v => parseInt(v, 2).toString(36),
    },
    "octal": {
        "binary": v => parseInt(v, 8).toString(2),
        "octal": v => v,
        "decimal": v => parseInt(v, 8).toString(10),
        "duodecimal": v => parseInt(v, 8).toString(12),
        "hexadecimal": v => parseInt(v, 8).toString(16),
        "vigesimal": v => parseInt(v, 8).toString(20),
        "tetravigesimal": v => parseInt(v, 8).toString(24),
        "duotrigesimal": v => parseInt(v, 8).toString(32),
        "hexatrigesimal": v => parseInt(v, 8).toString(36),
    },
    "decimal": {
        "binary": v => v.toString(2),
        "octal": v => v.toString(2),
        "decimal": v => v,
        "duodecimal": v => v.toString(12),
        "hexadecimal": v => v.toString(16),
        "vigesimal": v => v.toString(20),
        "tetravigesimal": v => v.toString(24),
        "duotrigesimal": v => v.toString(32),
        "hexatrigesimal": v => v.toString(36),
    },
    "duodecimal": {
        "binary": v => parseInt(v, 12).toString(2),
        "octal": v => parseInt(v, 12).toString(8),
        "decimal": v => parseInt(v, 12),
        "duodecimal": v => v,
        "hexadecimal": v => parseInt(v, 12).toString(16),
        "vigesimal": v => parseInt(v, 12).toString(20),
        "tetravigesimal": v => parseInt(v, 12).toString(24),
        "duotrigesimal": v => parseInt(v, 12).toString(32),
        "hexatrigesimal": v => parseInt(v, 12).toString(36),
    },
    
}

    /* Calculate sum */

const listFrom = document.getElementById("listFrom");
const listTo = document.getElementById("listTo");

inputPlace.addEventListener("keyup", function calculateInput(){
    calculate();
});

listFrom.addEventListener("change", function calculateFrom(){
    calculate();
});

listTo.addEventListener("change", function calculateTo(){
    calculate();
});

function calculate() {
    const listFromV = document.getElementById("listFrom").value;
    const listToV = document.getElementById("listTo").value;
    const inputPlace = parseFloat(document.getElementById("inputPlace").value);
    
    const fn = config[listFromV][listToV];
    document.getElementById("resultPlace").innerHTML = fn(inputPlace);
    
    if (document.getElementById("inputPlace").value == "" || document.getElementById("inputPlace").value == 0) {
        document.getElementById("resultPlace").innerHTML = "0.00";
    }
};

Is there a way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文