One thing that is common knowledge about JavaScript and other languages is that floating point numbers are not always stored the way you think. Also doing math with these numbers is even more iffy. One thing I recently wanted to do was effectively the opposite of parseInt(). In other words, I just wanted the fractional part of the passed number to be returned. Thusly the following function was born:


/**
 * @license Copyright 2014 - Chris West - MIT Licensed
 */
(function(RGX) {
    frac = function(num) {
        return +(+num).toExponential().replace(RGX, function(m, neg, num, dot, offset) {
            var zeroes = Array(Math.abs(offset) + 2).join('0');
            num = (zeroes + num + (dot ? '' : '.') + zeroes).split('.');
            return +(neg + '.' + num.join('').slice(+offset + num[0].length));
        });
    };
})(/(-?)(\d+(\.?)\d*)e(.+)/);

I really got the idea from the first programming language I ever used: that of the Casio graphing calculators. The following are example calls and results for the function:


var a = frac(1.234);     // 0.234
var b = frac(56789e-3);  // 0.789
var c = frac(12345678);  // 0
var d = frac(-34.5697);  // -0.5697
var e = frac('-.9');     // -0.9
var f = frac(null);      // 0
var g = frac(undefined); // NaN
var h = frac('sdfa');    // NaN

Personally I thought it was strange that this function isn’t natively provided in most languages but there are some alternatives. One that works in some languages but not in JavaScript (nor in Python interestingly enough) is the following:


a = 34.5697;
fracPart = a % 1;
alert(fracPart);  // 0.5696999999999974

Another alternative method that seems like it should work but doesn’t in JavaScript is as follows:


a = 34.5697;
fracPart = a - parseInt(a);
alert(fracPart);  // 0.5696999999999974

For this reason, if you have a need to pull the decimal portion of a number out, feel free to use the frac function defined at the beginning of this post. 😎


9 Comments

ildar · April 4, 2014 at 2:05 AM


a = 34.5697;
fracPart = a % 1;
alert(fracPart);

This is absolutely correct solution in JS. Check, for example with the result of 34.5697 - 34.

    Chris West · April 4, 2014 at 9:18 AM

    I just tried that code in the Google Chrome console and was given: 0.5696999999999974. The fact that the rounding isn’t done correctly is a known issue and is the reason why I’m using multiple steps to get to the answer.

      ildar · April 4, 2014 at 4:05 PM

      This issue is out of JS but in the approach of storing numbers in the limited set of bits. For example, 0.4 = (1/4+1/8+1/64+1/128+...) and so forth up to 32 bits (as it is true for JS). It means that if you see "0.4" the JS-engine itself has produced rounding of "0.3999..." up to "0.4".

        evan · May 22, 2014 at 8:43 PM

        Yes, this is due to the inherent error from arithmetic in floating point number systems.

        Your code is fun, but how about this:

        function frac(x) {
        var str = “” + x, idx = str.indexOf(“.”);
        return ~idx? +(“0” + str.substr(idx)) : 0;
        }

          evan · May 22, 2014 at 8:47 PM

          p.s. didn’t use so formatting is screwy; also didn't account for cases like x < 0, etc.

          Chris West · May 27, 2014 at 11:03 PM

          I always appreciate an alternative viewpoint. If I didn’t have to account for negative numbers and numbers (or strings) like 23.4e-5 I would definitely be down for using your solution with one alteration: slice instead of substr just to cut down on 1 more character. 🙂

    Keith · July 11, 2014 at 5:17 PM

    This would also work…

    var a = 34.5697;
    var fracPart = a – Math.floor(a);
    alert(fracPart);

      Chris West · July 13, 2014 at 12:24 AM

      Unfortunately this doesn’t work in all browsers as you would expect. For instance in Chrome it displays 0.5696999999999974 on both my Mac and PC. This the reason I wrote this function.

Alex Nash · July 18, 2019 at 12:08 PM

There is a simpler solution: just replace the whole part using `Math.trunc()`.
I’ve published typescript implementation in a public gist:
https://gist.github.com/Nachasic/21259aae50d0c798b5c28edb3547b318

Leave a Reply to ildar Cancel reply

Your email address will not be published. Required fields are marked *