1.1.0 • Published 2 months ago

round-number-cli v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

Round

JavaScript does some very crazy things with rounding.

TL,DR; Use the default method to round sanely and get back a 2-decimal place string.

Most of us were taught in school to use the 'round half up' method, like so:

Round 3.14159 to 1 decimal place

=> 3.1

Round 3.14159 to 2 decimal places

=> 3.14

Round 3.14159 to 3 decimal places

=> 3.142

Round 3.14159 to 4 decimal places

=> 3.1416

If you are rounding to 'x' places, and the digit in the 'x+1' position is equal to or greater than 5, round the 'x-th' digit up. Otherwise just drop the extra decimal places.

This module helps you do just that. It also provides more granular methods if you prefer a different rounding strategy.

Installation

npm install simple-round

API

simple-round can return your rounded number as either a number or string, depending on which method you pick.

String output

fixedSimpleRound(number)

The default method both rounds half-up to 2 decimal places and returns the result as a string, formatted to 2 decimal places. This is different than using JavaScript's Number.toFixed() method, which sometimes rounds up and sometimes rounds down (?), or Math.round(), which does things you might not expect, including returning your result in scientific notation, because...?

The {braces} are required in the require:

const {fixedSimpleRound} = require('simple-round');

expect(fixedSimpleRound(3.14159)).toEqual('3.14');

fixedRound({number, direction, precision})

Need more control? Yes, the {braces} are required, in both the require and the method call:

Params:

  • precision (non-negative)
  • number
  • direction (from the set below)
const {fixedRound, DIRECTIONS} = require('simple-round');

let number = 0.9999;
let direction = DIRECTIONS.DOWN;
let precision = 3;

expect(fixedRound({number, direction, precision})).toEqual('0.999');

let direction = DIRECTIONS.UP;

expect(fixedRound({number, direction, precision})).toEqual("1.000");

Default behavior for fixedRound is rounding half-up, to 2 decimal places. Omit either or both of those arguments if that's OK with you.

Number output

These two methods return your result as a number. The rounding will be correct, but leading or trailing zeros might be dropped.

simpleRound(number)

Supply the number argument. Rounds HALF_UP to 2 decimal places:

const {simpleRound} = require('simple-round');

expect(simpleRound(3.14159)).toEqual(3.14);

expect(simpleRound(2.345)).toEqual(2.35);

expect(simpleRound(0.999)).toEqual(1.00);

expect(simpleRound(0.285)).toEqual(0.29);

round({number, direction, precision})

Params as above. Yes, the {braces} are required, in both the require and the method call:

const {round, DIRECTIONS} = require('simple-round');

/*
DIRECTIONS.UP
DIRECTIONS.DOWN
DIRECTIONS.HALF_UP
DIRECTIONS.HALF_DOWN
 */


let number = 3.14159;
let direction = DIRECTIONS.DOWN;
let precision = 3;

expect(round({number, direction, precision})).toEqual(3.141);

let direction = DIRECTIONS.UP;

expect(round({number, direction, precision})).toEqual(3.142);

let direction = DIRECTIONS.HALF_UP;

expect(round({number, direction, precision})).toEqual(3.142);

let direction = DIRECTIONS.HALF_DOWN;

expect(round({number, direction, precision})).toEqual(3.141);

Default behavior for round() is to round HALF_UP to 2 decimal places. Omit either or both of those arguments if that's OK with you.

Testing

npm run test
slicks-bee-miss-stellanatgeo-wallpaper-bill-cartwrightdevsig-audrey-ratkesignalk-scheduler-claude-kozeybotlists.js-allen-stantonbotlists.js-allison-prohaskabotlists.js-amy-feilbotlists.js-angelo-collins-okunevabotlists.js-annie-corkerybotlists.js-annie-starkbotlists.js-beatrice-braunbotlists.js-bennie-leuschkebotlists.js-bennie-raynorbotlists.js-beulah-bergnaumbotlists.js-bobbie-turcottebotlists.js-cristina-sauerbotlists.js-cynthia-schinnerbotlists.js-daisy-ratkebotlists.js-daniel-corwinbotlists.js-dawn-stromanbotlists.js-deanna-stark-greenholtbotlists.js-deborah-bahringerbotlists.js-deborah-raubotlists.js-denise-lindgrenbotlists.js-desiree-casperbotlists.js-dr.-cameronbotlists.js-dr.-clintbotlists.js-dr.-dexterbotlists.js-dr.-joannabotlists.js-dr.-lorrainebotlists.js-dr.-simonbotlists.js-adam-kuhic-abshirebotlists.js-gregg-gerlachbotlists.js-gregg-gleichnerbotlists.js-gregory-cruickshankbotlists.js-gwen-crooksbotlists.js-harry-dubuquebotlists.js-herman-bashirianbotlists.js-homer-bahringerbotlists.js-isabel-mannbotlists.js-ismael-kihnbotlists.js-jamie-kohlerbotlists.js-jana-hermannbotlists.js-jean-whitebotlists.js-jenna-abbottbotlists.js-jessica-langbotlists.js-jo-durganbotlists.js-johnny-douglasbotlists.js-jon-ziemannbotlists.js-kari-haagbotlists.js-kelli-sanfordbotlists.js-kent-mitchellbotlists.js-laura-christiansenbotlists.js-leah-douglasbotlists.js-leon-bayerbotlists.js-leroy-leuschkebotlists.js-lillie-sporerbotlists.js-lorene-koeppbotlists.js-mack-kihnbotlists.js-marilyn-jaskolskibotlists.js-mindy-bechtelarbotlists.js-miss-annabotlists.js-miss-triciabotlists.js-molly-sipesbotlists.js-moses-klockobotlists.js-mr.-amosbotlists.js-mr.-jorgebotlists.js-alan-walterbotlists.js-alberto-bergebotlists.js-bradford-kovacekbotlists.js-brett-ornbotlists.js-calvin-strosinbotlists.js-cameron-paucekbotlists.js-carolyn-pagacbotlists.js-carrie-stantonbotlists.js-casey-schneiderbotlists.js-chester-hirthebotlists.js-cora-cristbotlists.js-craig-bashirianbotlists.js-emma-boscobotlists.js-essie-danielbotlists.js-adrienne-feilbotlists.js-mrs.-juanitabotlists.js-ms.-francesbotlists.js-ms.-maureenbotlists.js-ms.-moniquebotlists.js-myra-stiedemannbotlists.js-norman-fisherbotlists.js-patrick-jenkinsbotlists.js-penny-fritschbotlists.js-ramon-smithbotlists.js-randy-grimesbotlists.js-robyn-homenickbotlists.js-roderick-pfannerstillbotlists.js-roger-wymanbotlists.js-ross-hodkiewiczbotlists.js-rudy-kohlerbotlists.js-sean-lubowitzbotlists.js-shannon-kshlerinbotlists.js-sharon-turcotte
1.1.0

2 months ago

1.0.0

2 months ago