Design & Download
Your JS Library
in Seconds!!!

Snippet: Rounding with Precision

Written by Christopher West (cwest) on November 20, 2015.
Allows for rounding with precision with minimal risk of floating-point rounding issues.
'ceil floor round'.replace(/\w+/g, function(fnName, RGX_NUM_BOUNDS, RGX_EXTRA_DOT) {
  RGX_NUM_BOUNDS = /^-?|$/g;
  RGX_EXTRA_DOT = /(\.\d*)\./;
  YourJS[fnName] = function(num, opt_precision) {
    opt_precision = opt_precision || 0;
    var absPrecision = Math.abs(opt_precision);
    var zeroesSub = '$&' + new Array(absPrecision + 2).join('0');
    var rgxGrow = new RegExp('(\\.)(\\d{' + absPrecision + '})');
    var rgxShrink = new RegExp('(\\d{' + absPrecision + '})(\\.)');
    num = (num + '.').replace(RGX_NUM_BOUNDS, zeroesSub)
      .replace(RGX_EXTRA_DOT, '$1')
      .replace(opt_precision < 0 ? rgxShrink : rgxGrow, '$2$1');
    num = (Math[fnName](+num) + '.').replace(RGX_NUM_BOUNDS, zeroesSub)
      .replace(RGX_EXTRA_DOT, '$1')
      .replace(opt_precision < 0 ? rgxGrow : rgxShrink, '$2$1');
    return +num;
  };
});

A while back I wrote a short blog post about this very topic. Unfortunately, due to the incomplete representation of floating point numbers in JavaScript, my rounding functions didn't always return the expected result. After working through a more string-based solution I came up with the above buildable snippet which adds YourJS.ceil(), YourJS.floor() and YourJS.round().

YourJS.ceil(...) API Documentation

Description

Rounds towards Infinity with the specified precision.

Parameters

  1. num {number}:
    The number to be rounded towards Infinity.
  2. opt_precision {number}:
    Optional. Defaults to 0. The precision (as an integer) with which to round the number towards Infinity. Using the precision is similar to doing Math.ceil(num * Math.pow(10, precision)) / Math.pow(10, precision). Eg. YourJS.ceil(1.23, 1) -> 1.3 and YourJS.ceil(123.456, -1) -> 130.

Returns

Returns num rounded towards -Infinity with the specified precision.

YourJS.floor(...) API Documentation

Description

Rounds towards -Infinity with the specified precision.

Parameters

  1. num {number}:
    The number to be rounded towards Infinity.
  2. opt_precision {number}:
    Optional. Defaults to 0. The precision (as an integer) with which to round the number towards -Infinity. Using the precision is similar to doing Math.floor(num * Math.pow(10, precision)) / Math.pow(10, precision). Eg. YourJS.floor(1.23, 1) -> 1.2 and YourJS.floor(123.456, -1) -> 120.

Returns

Returns num rounded towards -Infinity with the specified precision.

YourJS.round(...) API Documentation

Description

Rounds with the specified precision.

Parameters

  1. num {number}:
    The number to be rounded.
  2. opt_precision {number}:
    Optional. Defaults to 0. The precision (as an integer) with which to round the number. Using the precision is similar to doing Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision). Eg. YourJS.round(1.55, 1) -> 1.6 and YourJS.round(153, -1) -> 150.

Returns

Returns num rounded with the specified precision.