Skip to content
Advertisement

Is there any way to see a number in it’s 64 bit float IEEE754 representation

Javascript stores all numbers as double-precision 64-bit format IEEE 754 values according to the spec:

The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic

Is there any way to see the number in this form in Javascript?

Advertisement

Answer

Based on @Pointy’s suggestion I’ve implemented the following function to get a number in it’s 64 bit float IEEE754 representation:

function to64bitFloat(number) {
    var f = new Float64Array(1);
    f[0] = number;
    var view = new Uint8Array(f.buffer);
    var i, result = "";
    for (i = view.length - 1; i >= 0; i--) {
        var bits = view[i].toString(2);
        if (bits.length < 8) {
            bits = new Array(8 - bits.length).fill('0').join("") + bits;
        }
        result += bits;
    }
    return result;
}

console.log(to64bitFloat(12)); // 0100000000101000000000000000000000000000000000000000000000000000
console.log(to64bitFloat(-12)); // 1100000000101000000000000000000000000000000000000000000000000000
Advertisement