Home:ALL Converter>Binary Floating point to decimal JavaScript function

Binary Floating point to decimal JavaScript function

Ask Time:2021-10-09T07:26:21         Author:john5634

Json Formatter

To answer the original question that was closed due to an invalid moderation. Binary Floating point to decimal JavaScript fucntion

This function below basically does the math based on the floating point logic.

Does anyone know of a built in function in JavaScript to do this? or a more straight forward way?

function fp32BinToDec(inputFpBinDiv) {
  var inputFpBin = document.getElementById(inputFpBinDiv).value;

  var expBin = inputFpBin.substring(1, 9);
  var mantBin = inputFpBin.substring(9, 33);

  var expDec = (parseInt(expBin, 2) - 127);

  var mantDec = (parseInt(mantBin, 2) / (2 ** 23));

  var dec = ((1 + mantDec) * (2 ** (expDec)));

  document.getElementById('output').innerHTML = dec;
  console.log(dec);
  return dec;
}
<!DOCTYPE html>
<html>

<head>
  <title>Floating point Binary to Decimal</title>
  <script src="site.js"></script>
</head>

<body>
  <button onclick="fp32BinToDec('inputField')">Convert to Decimal</button><br>
  <input id="inputField" value="01000100100110011010000000000000"></input><br>
  <p>Output: </p>
  <div id="output"></div><br>
  <p>Default Floating point binary should result a decimal of 1129</p>
</body>

</html>

Author:john5634,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69502608/binary-floating-point-to-decimal-javascript-function
yy