Skip to content

Commit

Permalink
fix vuejs#541 currency filter for negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 5, 2014
1 parent 447fa2d commit 23b596f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ exports.currency = function (value, sign) {
value = parseFloat(value)
if (!value && value !== 0) return ''
sign = sign || '$'
var s = Math.floor(value).toString(),
var s = Math.floor(Math.abs(value)).toString(),
i = s.length % 3,
h = i > 0
? (s.slice(0, i) + (s.length > 3 ? ',' : ''))
: '',
f = '.' + value.toFixed(2).slice(-2)
return sign + h + s.slice(i).replace(digitsRE, '$1,') + f
return (value < 0 ? '-' : '') +
sign + h + s.slice(i).replace(digitsRE, '$1,') + f
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/unit/specs/filters_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ describe('Filters', function () {
expect(filter(false)).toBe('')
expect(filter(null)).toBe('')
expect(filter(undefined)).toBe('')
// negative numbers
expect(filter(-50)).toBe('-$50.00')
expect(filter(-150.43)).toBe('-$150.43')
expect(filter(-1500.4343434)).toBe('-$1,500.43')
})

it('key', function () {
Expand Down

0 comments on commit 23b596f

Please sign in to comment.