mirror of https://github.com/Chocobozzz/PeerTube
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.tspull/3592/head
parent
12c1e38df2
commit
22078471fb
|
@ -10,10 +10,25 @@ export class NumberFormatterPipe implements PipeTransform {
|
||||||
{ max: 1000000000, type: 'M' }
|
{ max: 1000000000, type: 'M' }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param x number
|
||||||
|
* @param n number of decimals to get (defaults to 1, needs to be >= 1)
|
||||||
|
*/
|
||||||
|
static getDecimalForNumber (x: number, n = 1) {
|
||||||
|
const v = x.toString().split('.')
|
||||||
|
const f = v[1] || ''
|
||||||
|
if (f.length > n) return +f.substr(0, n)
|
||||||
|
return +f
|
||||||
|
}
|
||||||
|
|
||||||
transform (value: number) {
|
transform (value: number) {
|
||||||
const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
|
const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]
|
||||||
const calc = Math.floor(value / (format.max / 1000))
|
const calc = value / (format.max / 1000)
|
||||||
|
const integralPart = Math.floor(calc)
|
||||||
|
const decimalPart = NumberFormatterPipe.getDecimalForNumber(calc)
|
||||||
|
|
||||||
return `${calc}${format.type}`
|
return integralPart < 10 && decimalPart > 0
|
||||||
|
? `${integralPart}.${decimalPart}${format.type}`
|
||||||
|
: `${integralPart}${format.type}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue