Skip to content

Commit b02cd9b

Browse files
committed
Added percent filter
Issue #84
1 parent 61e325e commit b02cd9b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/other/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import bytes from './bytes'
33
import pluralize from './pluralize'
44
import ordinal from './ordinal'
55
import number from './number'
6+
import percent from './percent'
67

78
export {
89
currency,
910
bytes,
1011
pluralize,
1112
ordinal,
12-
number
13+
number,
14+
percent
1315
}

src/other/percent.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import util from '../util/index'
2+
3+
/**
4+
* 12 => '12%'
5+
* 100 => '100%'
6+
* 1000 => '1000%'
7+
* 0.97 => '97%'
8+
*
9+
* @param {Number} value
10+
* @param {Number} decimals Decimal places (default: 2)
11+
*/
12+
function percent(value, decimals) {
13+
const globalOptions = (this && this.percent) ? this.percent : {}
14+
decimals = util.exist(decimals) ? decimals : globalOptions.decimalDigits
15+
decimals = (typeof decimals !== 'undefined') ? decimals : 0
16+
value = value === null || isNaN(value) ? 0 : value
17+
18+
if(value <= 1) {
19+
return `${(value * 100).toFixed(decimals)}%`
20+
}
21+
22+
return `${value.toFixed(decimals)}%`
23+
}
24+
25+
export default percent

0 commit comments

Comments
 (0)