Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including roundit with all npm packages installed. Try it out:

var roundit = require("roundit")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

roundit v1.0.1

simple rounding library

roundit

javascript rounding library

A quick and simple rounding library, without resorting to string conversion, manipulation or other 3rd party maths routines.

Usage:

  var round = require('roundit');

  console.log('Round 5.125 to Nearest 2dp: ' + round.nearest(5.125, 2));
  console.log('Round 5.125 to Ceil 2dp: ' + round.ceil(5.125, 2));
  console.log('Round 5.125 to Floor 2dp: ' + round.floor(5.125, 2));
  console.log('Round 5.125 to Fix 2dp: ' + round.fix(5.125, 2));
  console.log('Round 5.125 to Infinite 2dp: ' + round.infinite(5.125, 2));

Rounding Options

nearest

Standard rounding to nearest number, >= 0.5 up, < 0.5 down.

  round.nearest(value to round, decimal places)
  1.5 to 0 decimal places => 2
  1.4 to 0 decimal places => 1
ceil

Always round up to larger significant digit (toward positive infinity).

  round.ceil(value to round, decimal places)
  1.5 to 0 decimal places => 2
  -1.5 to 0 decimal places => -1
floor

Always round up to smaller significant digit (toward negative infinity).

  round.floor(value to round, decimal places)
  1.5 to 0 decimal places => 1
  -1.5 to 0 decimal places => -2
fix

Always round towards zero.

  round.fix(value to round, decimal places)
  1.5 to 0 decimal places => 1
  -1.5 to 0 decimal places => -1
infinite

Always round away from zero (toward +-infinity).

  round.infinite(value to round, decimal places)
  1.5 to 0 decimal places => 2
  -1.5 to 0 decimal places => -2

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free