The following code takes a decimal number and gives its IEEE 754 floating point representation. However, the code is not complete and I am totally fine because it does the job for me:

  1. It does not consider negative numbers.
  2. The input number is not a fraction. Its always in the form xx.yy
  3. As of now, it does not give representation in 32-bit or 64-bit format, it just gives exponent and significant value.

function toBinary (decimal) {
    return decimal.toString(2);

function getIntegerPart (binary){
    return binary.slice(0, binary.indexOf('.'));

function getFractionalPart (binary){
    return binary.slice(binary.indexOf('.')+1);

function toNormalize (binary) {
    var normalizedRep = new Object();
    exponentValue = binary.indexOf('.')-1;
    floatPosition = binary.indexOf('.');
    normalizedRep.floatRep = binary[0]+ '.' + binary.slice(1, floatPosition) + binary.slice(floatPosition+1); 
    normalizedRep.exponentValue = exponentValue;
    normalizedRep.floatPosition = floatPosition;
    return normalizedRep;

function getSignificand (normalizedRep) {
    return normalizedRep.floatRep.slice(normalizedRep.floatRep.indexOf('.')+1);

function twosComp() {
    var decimal = 15.22;
    var binary = toBinary(decimal);
    var integerPart = getIntegerPart(binary);
    var fractionalPart = getFractionalPart(binary);
    var floatPosition = binary.indexOf('.');
    var normalizedRep = toNormalize(binary);
    var significand = getSignificand(normalizedRep);
    var exponentValue = normalizedRep.exponentValue;


1 Answer 1


I just don't get what you're trying to do. JavaScript does not know of integers, floats or any other numeric type, except for Number.
This data-type just happens to be a 64bit IEEE754 floating point value (or double in most strong-typed languages). This is clearly described in the official language specs

primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value

NOTE A Number value is a member of the Number type and is a direct representation of a number.

So a float representation of a number is the number itself. If you want n number of significant digits, then there's a handy method for that:

var num = 123.321;

If you want the integer part of a non-integer number:

parseInt(num, 10);//123 (best to specify the radix)

If you want the remainder, either one of the following approaches will do:

num - parseInt(num, 10);
num%1//modulo || remainder of division by 1

Note that in this last case, you will see the rounding errors of the IEE754 spec show up, so:

num = 123.321;
//get length of string rep for total number - int length,  -1 for .
var fix = (""+num).length - (""+parseInt(num, 10)).length - 1;

Might be required.

As far as the fraction representation of a number is concerned, that is going to take some work. Thankfully, it's all been done before and the code is available on github.

I don't know what you mean by: "It does not consider negative numbers", because, like I said before: there is but one type for numbers in JS, and so it's always signed. Just use the absolute value, and keep track of the what the number was initially (prositive or negative) like so:

function processNumber(num)
    var sign = !(num > 0);//bool, true if num is negative
    var workVal = Math.abs(num);//get absolute value
    var returnObj = {};
    returnObj.intVal = parseInt(workVal, 10);
    returnObj.floatPart = (workVal%1).toFixed((""+workVal).length - (""+returnObj.intVal).length - 1);
    returnObj.fixed2 = workVal.toFixed(2);
    //and so on...
    if (sign === true)
        for (prop in returnObj)
            if (returnObj.hasOwnProperty(prop))
            {//they're all strings, just prepend the sign...
                returnObj[prop] = '-' + returnObj[prop];
    return returnObj;
  • \$\begingroup\$ Seems I should have been clear about my code, apologizes for that. What my code does is, when you give a decimal number as input, it will give you its normalized representation with value of exponent and significand which will be stored in memory. The problem here I am trying to solve is, when you give a decimal number, how it will be stored in memory in IEEE-754 format. \$\endgroup\$
    – avi
    Commented Oct 31, 2013 at 11:23
  • 2
    \$\begingroup\$ @avi: That's exactly why I kicked off my answer with a link to the ECMA specification. All numbers are stored in IEEE-754 64bit numbers. With all quirks this format that entails. \$\endgroup\$ Commented Oct 31, 2013 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.