Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,25 @@ export default {
* 12345 => $12,345.00
*
* @param {String} sign
* @param {Number} decimals Decimal places
*/

currency (value, currency) {
currency (value, currency, decimals) {
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
var stringified = Math.abs(value).toFixed(2)
var _int = stringified.slice(0, -3)
decimals = decimals != null ? decimals : 2
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals
? stringified.slice(0, -1 - decimals)
: stringified
var i = _int.length % 3
var head = i > 0
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
: ''
var _float = stringified.slice(-3)
var _float = decimals
? stringified.slice(-1 - decimals)
: ''
var sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
Expand Down
6 changes: 6 additions & 0 deletions test/unit/specs/filters/filters_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ describe('Filters', function () {
expect(filter(2134, '@')).toBe('@2,134.00')
// no symbol
expect(filter(2134, '')).toBe('2,134.00')
// decimal places
expect(filter(1234, '$', 0)).toBe('$1,234')
// if decimal places are present, currency is required
expect(filter(1234, '', 2)).toBe('1,234.00')
expect(filter(123.4, '$', 3)).toBe('$123.400')
expect(filter(-12345, 'VND', 0)).toBe('-VND12,345')
// falsy, infinity and 0
expect(filter(0)).toBe('$0.00')
expect(filter(false)).toBe('')
Expand Down