How can I convert binary float number into a decimal float number ?
How can I convert binary float number into a decimal float number ?
Collapse
X
-
Tags: None
-
-
in JavaScript there is exactly one number type: float (IEEE 774). that is, a binary number must be a string consisting of 0s and 1s. for string conversion you have 3 methods:
- Number() (the constructor for the Number objects)
- parseFloat() (parse a string into a float (obviously a decimal float))
- parseInt() (parse a string as integer into a float using the given radix)
thus if you have any number that’s not decimal, you have to apply parseInt(), as that’s the only function where you can set the radix.Comment
-
Thank you sir I understood.
But please check is it possible to develope the script below, to convert binary float number in to decimal float number.
Code:<html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <script type="text/javascript"> function ConvertToBinary(){ var numberValue = document.getElementById('NumberInput').value; var decNumber = Number(numberValue); var binaryNumber = decNumber.toString(2).toUpperCase(); document.getElementById('Result').value = binaryNumber; } function ConvertToDec(){ var binaryNumber = document.getElementById('NumberInput').value; var decNumber = parseInt(binaryNumber, 2); document.getElementById('Result').value = decNumber; } </script> <div style="text-align:center"> Number: <input type="text" id="NumberInput"></input> Result: <input type="text" id="Result"></input> <br/> <button onclick="ConvertToBinary();">Convert To Binary</button> <button onclick="ConvertToDec();">Convert To Decimal</button> <br /> </div> </body> </html>Comment
-
Thank you sir I understood.
But please check is it possible to develope the script below, to convert binary float number in to decimal float number.
Code:<html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <script type="text/javascript"> function ConvertToBinary(){ var numberValue = document.getElementById('NumberInput').value; var decNumber = Number(numberValue); var binaryNumber = decNumber.toString(2).toUpperCase(); document.getElementById('Result').value = binaryNumber; } function ConvertToDec(){ var binaryNumber = document.getElementById('NumberInput').value; var decNumber = parseInt(binaryNumber, 2); document.getElementById('Result').value = decNumber; } </script> <div style="text-align:center"> Number: <input type="text" id="NumberInput"></input> Result: <input type="text" id="Result"></input> <br/> <button onclick="ConvertToBinary();">Convert To Binary</button> <button onclick="ConvertToDec();">Convert To Decimal</button> <br /> </div> </body> </html>Comment
Comment