Skip to content

Commit 452975b

Browse files
committed
Added showPlusSign option to currency filter
Resolve #76
1 parent e5ed190 commit 452975b

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ export default {
248248
* `{String} [decimalSeparator] - default: '.'`
249249
* `{Boolean} [symbolOnLeft] - default: true`
250250
* `{Boolean} [spaceBetweenAmountAndSymbol] - default: false`
251+
* `{Boolean} [showPlusSign] - default: false`
251252

252253
+ Example:
253254

@@ -284,6 +285,12 @@ export default {
284285
```js
285286
{{ amount | currency('$', 0, { spaceBetweenAmountAndSymbol: true }) }} // 12345 => $ 12,345
286287
```
288+
289+
Show the plus sign if the value is greater than zero:
290+
291+
```js
292+
{{ amount | currency('$', 0, { showPlusSign: true }) }} // 12345 => +$12,345
293+
```
287294
Use multiple options:
288295

289296
```js
@@ -468,7 +475,8 @@ var Vue2FiltersConfig = {
468475
thousandsSeparator: ',',
469476
decimalSeparator: '.',
470477
symbolOnLeft: true,
471-
spaceBetweenAmountAndSymbol: false
478+
spaceBetweenAmountAndSymbol: false,
479+
showPlusSign: false
472480
},
473481
pluralize: {
474482
includeNumber: false

src/other/currency.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function currency (value, symbol, decimals, options) {
1414
symbol = util.exist(symbol) ? symbol : globalOptions.symbol
1515
decimals = util.exist(decimals) ? decimals : globalOptions.decimalDigits
1616
options = options || globalOptions
17-
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol
17+
var thousandsSeparator, symbolOnLeft, spaceBetweenAmountAndSymbol, showPlusSign
1818
var digitsRE = /(\d{3})(?=\d)/g
1919
value = parseFloat(value)
2020
if (!isFinite(value) || (!value && value !== 0)) return ''
@@ -23,6 +23,7 @@ function currency (value, symbol, decimals, options) {
2323
thousandsSeparator = options.thousandsSeparator != null ? options.thousandsSeparator : ','
2424
symbolOnLeft = options.symbolOnLeft != null ? options.symbolOnLeft : true
2525
spaceBetweenAmountAndSymbol = options.spaceBetweenAmountAndSymbol != null ? options.spaceBetweenAmountAndSymbol : false
26+
showPlusSign = options.showPlusSign != null ? options.showPlusSign : false
2627
var number = Math.abs(value)
2728
var stringified = toFixed(number, decimals)
2829
stringified = options.decimalSeparator
@@ -47,7 +48,8 @@ function currency (value, symbol, decimals, options) {
4748
: head +
4849
_int.slice(i).replace(digitsRE, '$1' + thousandsSeparator) + _float + symbol
4950
var sign = value < 0 ? '-' : ''
50-
return sign + symbol
51+
var plusSign = (value > 0 && showPlusSign) ? '+' : ''
52+
return plusSign + sign + symbol
5153
}
5254

5355
function toFixed(num, precision) {

test/filters.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ describe('Filters', function() {
145145
expect(filter(1234, '$', 0, {spaceBetweenAmountAndSymbol: true})).toBe('$ 1,234')
146146
expect(filter(1234, '$', 0, {symbolOnLeft: false,spaceBetweenAmountAndSymbol: true})).toBe('1,234 $')
147147
expect(filter(-12345, 'VND', 0, {symbolOnLeft: true})).toBe('-VND12,345')
148+
expect(filter(12345, 'VND', 0, {showPlusSign: true})).toBe('+VND12,345')
149+
expect(filter(-12345, 'VND', 0, {showPlusSign: true})).toBe('-VND12,345')
150+
expect(filter(0, 'VND', 0, {showPlusSign: true})).toBe('VND0')
148151
// round up
149152
expect(filter(4514.275)).toBe('$4,514.28')
150153
expect(filter(9446.975)).toBe('$9,446.98')
@@ -158,11 +161,12 @@ describe('Filters', function() {
158161
thousandsSeparator: ',',
159162
decimalSeparator: '|',
160163
symbolOnLeft: false,
161-
spaceBetweenAmountAndSymbol: true
164+
spaceBetweenAmountAndSymbol: true,
165+
showPlusSign: true
162166
}
163167
})
164168

165-
expect(filter(1234)).toBe('1,234|000 @')
169+
expect(filter(1234)).toBe('+1,234|000 @')
166170
})
167171

168172
it('pluralize', function() {

0 commit comments

Comments
 (0)