JSPM

compute-roundn

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 31
  • Score
    100M100P100Q62550F

Round values to the nearest multiple of 10^n.

Package Exports

  • compute-roundn

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (compute-roundn) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 © 2014. Athan Reines.