Round values to the nearest multiple of 10^n.
$ npm install compute-roundn
For use in the browser, use browserify.
To use the module,
var roundn = require( 'compute-roundn' );
Rounds values to the nearest multiple of 10^n
. x
may be either a numeric array
or a single numeric value. n
must be an integer
specifying the multiple of 10^n
to which the value(s) should be rounded.
var val = roundn( 2.4567, -2 );
// returns 2.46
// Round a value to 2 decimal places:
console.log( roundn( Math.PI, -2 ) );
// returns 3.14
// If `n=0`, then `roundn()` behaves like `Math.round()`:
console.log( roundn( Math.PI, 0 ) );
// returns 3
console.log( Math.round( Math.PI ) );
// returns 3
// Round a value to the nearest thousand:
console.log( roundn( 12368, 3 ) );
// returns 12000
// Round each array value to 2 decimal places...
var data = new Array( 5 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.PI;
}
console.log( roundn( data, -2 ) );
// returns [ 3.14, 3.14, 3.14, 3.14, 3.14 ]
To run the example code from the top-level application directory,
$ node ./examples/index.js
If provided an input array
, the array
is mutated. If mutation is undesired,
var data = [ Math.PI, Math.PI, Math.PI ],
copy = data.slice();
round( copy, -2 );
If provided an empty array
, the function returns null
.
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Copyright © 2014. Athan Reines.