Design & Download
Your JS Library
in Seconds!!!

Snippet: frac() - Fractional Part of a Number

Written by Christopher West (cwest) on November 24, 2015.
Gets the fractional part of a number, removing the integral part.
var frac;
(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));

This snippet gives us the ability to get the fractional part of a number. In other words, it will give you the part of the number after the decimal point, keeping the sign of the number.

frac(num) API Documentation


  1. num {number}:
    The number from which the fractional part will be pulled.


Returns num without the integral part while keeping the sign.