Skip to content

Commit

Permalink
Merge pull request #19 from goodeggs/feat/update-to-string
Browse files Browse the repository at this point in the history
feat: include decimal point param in toString()
  • Loading branch information
piercebb authored Dec 5, 2022
2 parents 552a967 + 99edde1 commit ac4f314
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ $ npm test
```

_Module scaffold generated by [generator-goodeggs-npm](https://github.com/goodeggs/generator-goodeggs-npm)._

## Releasing

To release a new version of this module, use yarn to bump the version
in `package.json` and create a git tag, then push. This will automatically
get published to the NPM registry via CI.

```sh
yarn version --new-version=<major|minor|patch|premajor|preminor|prepatch>
git push --follow-tags
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "goodeggs-money",
"version": "2.0.2",
"version": "2.1.0",
"description": "Reliable money math with BigNumber wrapped inside Cents",
"author": "Good Eggs <[email protected]>",
"contributors": [
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,14 @@ class Cents {
}

/**
* always show 2 decimal places
* show number with dollar sign and 2 decimal places, unless specified
*
* @param decimalPlaces the number of decimal places resolved
*
* @returns {string}
*/
toString(): string {
return `$${new BigNumber(this.toDollars()).toFixed(2)}`;
toString(decimalPlaces = 2): string {
return `$${new BigNumber(this.toDollars()).toFixed(decimalPlaces)}`;
}

// BigNumber.js removed `round()`, `ceil()`, and `floor()` in in v6.0.0. Previously this library
Expand Down
4 changes: 4 additions & 0 deletions test/goodeggs_money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,9 @@ describe('Cents', () => {
expect(new Cents(1).toString()).to.equal('$0.01');
expect(new Cents(10).toString()).to.equal('$0.10');
expect(new Cents(100).toString()).to.equal('$1.00');
expect(new Cents(1000).toString()).to.equal('$10.00');
expect(new Cents(1000).toString(0)).to.equal('$10');
expect(new Cents(1000).toString(1)).to.equal('$10.0');
expect(new Cents(1000).toString(3)).to.equal('$10.000');
}));
});

0 comments on commit ac4f314

Please sign in to comment.