Skip to content

Commit

Permalink
add rounding type option
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Aug 28, 2018
1 parent 457776b commit 3b1a4b9
Show file tree
Hide file tree
Showing 14 changed files with 3,622 additions and 347 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 0.1.0 - 2018-08-28
- **BREAKING** changed default rounding type from `significant`
- Add `rounding` option, now rounding type can be specified: `default`, `significant`, `fixed`
- Fixed ending zeros not stripped after rounding

## 0.0.1 - 2018-07-31
- Initial
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,44 @@ prettyNum(12.123e-10); // => '0.0000000012123'
prettyNum(0.00123456, {precision: 3}); // => '0.00123'
prettyNum(12345678.12345, {thousandsSeparator: ' '}); // => '12 345 678.12345'
prettyNum('00123456789.12300e-2', {precision: 3, thousandsSeparator: ' '}); // => '1 234 567.891'
```

### Options

#### thousandsSeparator
Defines the thousand grouping separator character

#### precision
Number of decimal digits to keep when rounding. Pass falsey value to not change precision.

#### rounding
Rounding type
- `'default'` - reduce precision to specified number of decimal digits, strip unnecessary ending zeros.

```js
prettyNum(0.01023456, {precision: 3});
// => '0.01'
prettyNum(0.00001203456, {precision: 3});
// => '0'
```

- `'significant'` - reduce precision to specified number of significant decimal digits, strip unnecessary ending zeros. Useful when rounding small values and they should not be rounded to 0
```js
prettyNum(0.01023456, {precision: 3, rounding: 'significant'});
// => '0.0102'
prettyNum(0.00001203456, {precision: 3, rounding: 'significant'});
// => '0.000012'
```

- `'fixed'` - reduce precision to specified number of decimal digits, pad with ending zeros.
```js
prettyNum(0.01023456, {precision: 3, rounding: 'significant'});
// => '0.010'
prettyNum(0.00001203456, {precision: 3, rounding: 'fixed'});
// => '0.000'
```



## License

Expand Down
4 changes: 2 additions & 2 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default {
resolve(),
babel({
babelrc: false,
presets: [['env', { modules: false }]],
presets: [['@babel/preset-env', { modules: false }]],
}),
],
output: {
file: 'dist/index.js',
format: 'umd',
name: 'prettyNum',
}
};
};
4 changes: 2 additions & 2 deletions build/rollup.uglify.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
resolve(),
babel({
babelrc: false,
presets: [['env', { modules: false }]],
presets: [['@babel/preset-env', { modules: false }]],
}),
terser(), // uglifyjs alternative with es6 support
],
Expand All @@ -19,4 +19,4 @@ export default {
format: 'umd',
name: 'prettyNum',
}
};
};
Loading

0 comments on commit 3b1a4b9

Please sign in to comment.