Forked from maxkoretskyi/Representing a number as 64 bit float
Created
October 24, 2018 09:20
-
-
Save yanzhihong23/9affecdd54a4039b5a8a5ba18d560200 to your computer and use it in GitHub Desktop.
Representing a number as 64 bit float
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function to64bitFloat(number) { | |
var i, result = ""; | |
var dv = new DataView(new ArrayBuffer(8)); | |
dv.setFloat64(0, number, false); | |
for (i = 0; i < 8; i++) { | |
var bits = dv.getUint8(i).toString(2); | |
if (bits.length < 8) { | |
bits = new Array(8 - bits.length).fill('0').join("") + bits; | |
} | |
result += bits; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment