Skip to content

Commit

Permalink
Merge 0669e66 into 78b5a40
Browse files Browse the repository at this point in the history
  • Loading branch information
tarnelope committed Apr 18, 2018
2 parents 78b5a40 + 0669e66 commit bbed44d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/dinero.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,11 @@ const Dinero = options => {
*/
toRoundedUnit(precision) {
const factor = Math.pow(10, precision)
return calculator.divide(
Math.round(calculator.multiply(this.toUnit(), factor)),
factor
)
const sign = Math.sign(this.toUnit())
const roundedUnit =
sign * Math.round(Math.abs(calculator.multiply(this.toUnit(), factor)))

return calculator.divide(roundedUnit, factor)
},
/**
* Return the object's data as an object literal.
Expand Down
1 change: 1 addition & 0 deletions test/unit/dinero.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ describe('Dinero', () => {
describe('#toRoundedUnit()', () => {
test('should return the amount divided by 100, rounded to one fraction digit', () => {
expect(Dinero({ amount: 1055 }).toRoundedUnit(1)).toBe(10.6)
expect(Dinero({ amount: -1055 }).toRoundedUnit(1)).toBe(-10.6)
})
})
describe('#toObject()', () => {
Expand Down

0 comments on commit bbed44d

Please sign in to comment.