diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-01-13 09:15:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-13 09:15:43 +0100 |
commit | 22078471fbe5a4dea6177bd1fa19da1cf887679e (patch) | |
tree | 6bdf52162f25aa3f84541adea080c9ee763872b3 /client/src/app/shared | |
parent | 12c1e38df2fde0efbf948fa80e2afc4e67f0e8c9 (diff) | |
download | PeerTube-22078471fbe5a4dea6177bd1fa19da1cf887679e.tar.gz PeerTube-22078471fbe5a4dea6177bd1fa19da1cf887679e.tar.zst PeerTube-22078471fbe5a4dea6177bd1fa19da1cf887679e.zip |
show first decimal for views above a thousand (#3564)
* show first decimal for views above a thousand
* Update client/src/app/shared/shared-main/angular/number-formatter.pipe.ts
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/shared-main/angular/number-formatter.pipe.ts | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts b/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts index e2eba3a60..5e6ccfa16 100644 --- a/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts +++ b/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts | |||
@@ -10,10 +10,25 @@ export class NumberFormatterPipe implements PipeTransform { | |||
10 | { max: 1000000000, type: 'M' } | 10 | { max: 1000000000, type: 'M' } |
11 | ] | 11 | ] |
12 | 12 | ||
13 | /** | ||
14 | * @param x number | ||
15 | * @param n number of decimals to get (defaults to 1, needs to be >= 1) | ||
16 | */ | ||
17 | static getDecimalForNumber (x: number, n = 1) { | ||
18 | const v = x.toString().split('.') | ||
19 | const f = v[1] || '' | ||
20 | if (f.length > n) return +f.substr(0, n) | ||
21 | return +f | ||
22 | } | ||
23 | |||
13 | transform (value: number) { | 24 | transform (value: number) { |
14 | const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] | 25 | const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] |
15 | const calc = Math.floor(value / (format.max / 1000)) | 26 | const calc = value / (format.max / 1000) |
27 | const integralPart = Math.floor(calc) | ||
28 | const decimalPart = NumberFormatterPipe.getDecimalForNumber(calc) | ||
16 | 29 | ||
17 | return `${calc}${format.type}` | 30 | return integralPart < 10 && decimalPart > 0 |
31 | ? `${integralPart}.${decimalPart}${format.type}` | ||
32 | : `${integralPart}${format.type}` | ||
18 | } | 33 | } |
19 | } | 34 | } |