<style> html, body { margin: 0; } body { font-family: sans-serif; border-top: 5px solid #0074C1; } form { margin: 10px; } label { cursor: pointer; } </style> <form onsubmit="convert(this); return false"> <h1>Hexadecimal float to decimal float converter</h1> <p> <label for="f64"><input id="f64" name="precision" value="f64" type="radio" checked /> f64</label> <label for="f32"><input id="f32" name="precision" value="f32" type="radio" /> f32</label> <input id="pre" type="text" value="test(" /> <input id="post" type="text" value=");" /> <button>Convert</button> </p> <p><textarea cols="120" rows="20" id="input"></textarea></p> <p><textarea cols="120" rows="20" id="output" readonly></textarea></p> </form> <script src="hexfloat.js"></script> <script> function convert(form) { var isF64 = document.getElementById("f64").checked; var pre = document.getElementById("pre").value; var post = document.getElementById("post").value; var input = document.getElementById("input").value; document.getElementById("output").value = input .replace(/\b(\-?0x[0-9a-fA-F]*(?:\.[0-9a-fA-F]+)?[pP][+-]?[0-9]+\b)/g, ($0, $1) => { var val = parse($1); return val.toPrecision(isF64 ? 18 : 10); }) .replace(/(\d\.[0-9])0+\b/g, "$1") .replace(/\bnan\b/g, "NaN") .replace(/\binf\b/g, "Infinity") .replace(/^T\(R\w, */mg, pre) .replace(/\);?$/mg, post) .replace(/ +/g, " "); } </script>