Skip to content

Commit

Permalink
feat(dinero): Add toRoundedUnit method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Mar 13, 2018
1 parent fa81b0b commit 13ba7ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/dinero.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,20 @@ const Dinero = options => {
toUnit() {
return this.getAmount() / 100
},
/**
* Returns the amount represented by this object in rounded units.
*
* @example
* // returns 10.6
* Dinero({ amount: 1055 }).toRoundedUnit(1)
*
* @param {Number} precision - The number of fraction digits to round to.
* @return {Number}
*/
toRoundedUnit(precision) {
const factor = Math.pow(10, precision)
return Math.round(this.toUnit() * factor) / factor
},
/**
* Return the object's data as an object literal.
*
Expand Down
5 changes: 5 additions & 0 deletions test/unit/dinero.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ describe('Dinero', () => {
expect(Dinero({ amount: 1050 }).toUnit()).to.equal(10.5)
})
})
describe('#toRoundedUnit()', () => {
it('should return the amount divided by 100, rounded to one fraction digit', () => {
expect(Dinero({ amount: 1055 }).toRoundedUnit(1)).to.equal(10.6)
})
})
describe('#toObject()', () => {
it('should return an object literal with the right data', () => {
expect(Dinero({ amount: 500, currency: 'EUR' }).toObject()).to.deep.equal(
Expand Down

0 comments on commit 13ba7ba

Please sign in to comment.