[英]Number converter is not converting digits after decimal point
Actually I am a student and I had just started to programme in Javascript. 实际上,我是一名学生,我刚开始用Java编程。 I have created a binary to octal to hexadecimal to decimal converter which is working quite good but the digits after decimal point are not converted please help me as necessary 我创建了一个二进制到八进制到十六进制到十进制的转换器,它的效果很好,但是小数点后的数字没有转换,请根据需要帮助我
I can't figure out the problem 我不明白问题所在
My project codes 我的项目代码
<html>
<head>
<title> Convertor </title>
</head>
<body>
<input id="bin" oninput="Convert('bin', 2)" placeholder="bin" spellcheck="false">
<input id="oct" oninput="Convert('oct', 8)" placeholder="oct" spellcheck="false">
<input id="dec" oninput="Convert('dec', 10)" placeholder="dec" spellcheck="false">
<input id="hex" oninput="Convert('hex', 16)" placeholder="hex" spellcheck="false">
<script>
function id(id) {
return document.getElementById(id);
}
function Convert(s, n) {
if(parseInt(id(s).value, n)) {
if("bin" != s) { id("bin").value = parseInt(id(s).value, n).toString(2) }
if("oct" != s) { id("oct").value = parseInt(id(s).value, n).toString(8) }
if("dec" != s) { id("dec").value = parseInt(id(s).value, n).toString(10) }
if("hex" != s) { id("hex").value = parseInt(id(s).value, n).toString(16) }
} else {
if("bin" != s) { id("bin").value = "" }
if("oct" != s) { id("oct").value = "" }
if("dec" != s) { id("dec").value = "" }
if("hex" != s) { id("hex").value = "" }
}
}
</script>
</body>
Use parseFloat
instead of parseInt
to parse decimal numbers. 使用parseFloat
而不是parseInt
来解析十进制数字。 You cannot pass the second argument - the radix - when using parseFloat
. 使用parseFloat
时,不能传递第二个参数-基数。
Fathyb mentioned that parseFloat has no radix parameter. Fathyb提到parseFloat没有基数参数。 And there is a simple reason. 这是一个简单的原因。 What du you expect as result when converting 3.5 to binary? 将3.5转换为二进制时,您期望得到什么结果? 3 equals 0000011 and 5 is 0000101. But 0000011.00000101 is not a valid binary format. 3等于0000011,5等于0000101。但是0000011.00000101不是有效的二进制格式。
That means as far as i know your code is fine because floats are not supported. 就我所知,这意味着我的代码很好,因为不支持浮点数。 ;o) ; O)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.