By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

How can I convert binary float number into a decimal float number ?

P: 14
How can I convert binary float number into a decimal float number ?
Aug 16 '10 #1

✓ answered by Dormilich

you do this as integer via parseInt() by passing the inputís radix (second parameter). (binary numbers usually ainít floats)

Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you do this as integer via parseInt() by passing the inputís radix (second parameter). (binary numbers usually ainít floats)
Aug 16 '10 #2

P: 14
I am a beginner.So can you explain this that answer with an example.
Thanks
Aug 17 '10 #3

Dormilich
Expert Mod 5K+
P: 8,639
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.
Aug 17 '10 #4

P: 14
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.


Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2.      <head></head>
  3.   <body>
  4.  <script type="text/javascript">
  5.    function ConvertToBinary(){
  6.         var numberValue = document.getElementById('NumberInput').value;
  7.        var decNumber = Number(numberValue);
  8.          var binaryNumber = decNumber.toString(2).toUpperCase();
  9.  
  10.          document.getElementById('Result').value = binaryNumber;
  11.        }
  12.        function ConvertToDec(){
  13.          var binaryNumber = document.getElementById('NumberInput').value;
  14.        var decNumber = parseInt(binaryNumber, 2);
  15.          document.getElementById('Result').value = decNumber;
  16.       }
  17.     </script>
  18.  
  19.      <div style="text-align:center">
  20.        Number: <input type="text" id="NumberInput"></input>
  21.        Result: <input type="text" id="Result"></input>
  22.       <br/>
  23.       <button onclick="ConvertToBinary();">Convert To Binary</button>
  24.       <button onclick="ConvertToDec();">Convert To Decimal</button>
  25.      <br />
  26.     </div>
  27.    </body>
  28.  </html>
Aug 17 '10 #5

Dormilich
Expert Mod 5K+
P: 8,639
there’s one important thing you forgot:

© 2010 Frinavale


if you copy, copy right (pun intended)
Aug 17 '10 #6

P: 14
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.



Expand|Select|Wrap|Line Numbers
  1.     <html xmlns="http://www.w3.org/1999/xhtml"> 
  2.          <head></head>
  3.      <body>
  4.   <script type="text/javascript">
  5.      function ConvertToBinary(){
  6.      var numberValue = document.getElementById('NumberInput').value;
  7.          var decNumber = Number(numberValue);
  8.           var binaryNumber = decNumber.toString(2).toUpperCase();
  9.  
  10.          document.getElementById('Result').value = binaryNumber;
  11.          }
  12.        function ConvertToDec(){
  13.         var binaryNumber = document.getElementById('NumberInput').value;
  14.         var decNumber = parseInt(binaryNumber, 2);
  15.          document.getElementById('Result').value = decNumber;
  16.        }
  17.     </script>
  18.  
  19.       <div style="text-align:center">
  20.         Number: <input type="text" id="NumberInput"></input>
  21.         Result: <input type="text" id="Result"></input>
  22.       <br/>
  23.        <button onclick="ConvertToBinary();">Convert To Binary</button>
  24.        <button onclick="ConvertToDec();">Convert To Decimal</button>
  25.        <br />
  26.      </div>
  27.    </body>
  28.   </html>
Aug 19 '10 #7

Dormilich
Expert Mod 5K+
P: 8,639
I would assume Frinavale doesn’t publish code that’s not working.
Aug 19 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.