forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexfloat.html
40 lines (38 loc) · 1.42 KB
/
hexfloat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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>