JS中的数字系统转换(输入字母时错误)
我有一个转换器(数字系统)。一切都很好,但是当我试图将十二指肠(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论