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. 😎

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`

.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.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"`

.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;

}

p.s. didn’t use

`so formatting is screwy; also didn't account for cases like x < 0, etc.`

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. 🙂This would also work…

var a = 34.5697;

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

alert(fracPart);

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.