Conversion from Decimal to octal, binary and hexadecimal in javascript

前端 未结 5 1464
花落未央
花落未央 2021-02-08 17:23

I have a form where a user enters a decimal number and then from the dropdown menu he chooses if he wants to convert it either to binary, octal or hexadecimal by clicking on a c

5条回答
  •  清酒与你
    2021-02-08 17:57

    you can directly convert your values to binary and octal and hexadecimal like this

    function Answer() {
    
        if (document.getElementbyId ('selectid').value=="binary") {
            this.value = this.value.toString(2);
        }
        else if  (document.getElementbyId ('selectid').value=="octal") {
            this.value = this.value.toString(8);
        }
        else if  (document.getElementbyId ('selectid').value=="hexadecimal") {
            this.value = this.value.toString(16);
        }
    }
    

提交回复
热议问题