node package manager

compute-roundn

Round values to the nearest multiple of 10^n.

roundn

NPM version Build Status Coverage Status Dependencies

Round values to the nearest multiple of 10^n.

Installation

$ npm install compute-roundn

For use in the browser, use browserify.

Usage

To use the module,

var roundn = require( 'compute-roundn' );

roundn( x, n )

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 

Examples

// 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

Notes

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

Tests

Unit

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.

Test Coverage

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

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.