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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Udara chathuranga
    New Member
    • Aug 2010
    • 14

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

    How can I convert binary float number into a decimal float number ?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

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

    Comment

    • Udara chathuranga
      New Member
      • Aug 2010
      • 14

      #3
      Help more

      I am a beginner.So can you explain this that answer with an example.
      Thanks

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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

        • Udara chathuranga
          New Member
          • Aug 2010
          • 14

          #5
          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>
          Last edited by Niheel; Aug 17 '10, 06:44 PM. Reason: please use code tags to display code

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            there’s one important thing you forgot:

            © 2010 Frinavale


            if you copy, copy right (pun intended)
            Last edited by Dormilich; Aug 17 '10, 09:28 PM.

            Comment

            • Udara chathuranga
              New Member
              • Aug 2010
              • 14

              #7
              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>
              Last edited by Dormilich; Aug 19 '10, 08:08 AM. Reason: fixed [code] tags

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I would assume Frinavale doesn’t publish code that’s not working.

                Comment

                Working...