File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,13 @@ import bytes from './bytes'
3
3
import pluralize from './pluralize'
4
4
import ordinal from './ordinal'
5
5
import number from './number'
6
+ import percent from './percent'
6
7
7
8
export {
8
9
currency ,
9
10
bytes ,
10
11
pluralize ,
11
12
ordinal ,
12
- number
13
+ number ,
14
+ percent
13
15
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments