assemblyscript/scripts/hexfloat.html

38 lines
1.3 KiB
HTML

<style>
html, body { margin: 0; }
body { font-family: sans-serif; border-top: 5px solid #0074C1; }
form { margin: 10px; }
label { cursor: pointer; }
</style>
<script src="hexfloat.js"></script>
<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="name" type="text" value="test(" />
<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>
function convert(form) {
var isF64 = document.getElementById("f64").checked;
var name = document.getElementById("name").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(/\bnan\b/g, "NaN")
.replace(/\inf\b/g, "Infinity")
.replace(/^T\(RN, */mg, name + "(")
.replace(/\)$/mg, ");")
.replace(/ +/g, " ");
}
</script>