Display one decimal for metrics smaller than 10 (#3735)

* Display one decimal for metrics smaller than 10

* Update test validators

* Run Prettier

* Update GitHub regexes
This commit is contained in:
Pierre-Yves B
2019-07-17 20:00:29 +01:00
committed by GitHub
parent e556ac2793
commit 3dbe655611
10 changed files with 70 additions and 32 deletions

View File

@@ -56,9 +56,17 @@ function metric(n) {
for (let i = metricPrefix.length - 1; i >= 0; i--) {
const limit = metricPower[i]
if (n >= limit) {
n = Math.round(n / limit)
if (n < 1000) {
return `${n}${metricPrefix[i]}`
const scaledN = n / limit
if (scaledN < 10) {
// For "small" numbers, display one decimal digit unless it is 0.
const oneDecimalN = scaledN.toFixed(1)
if (oneDecimalN.charAt(oneDecimalN.length - 1) !== '0') {
return `${oneDecimalN}${metricPrefix[i]}`
}
}
const roundedN = Math.round(scaledN)
if (roundedN < 1000) {
return `${roundedN}${metricPrefix[i]}`
} else {
return `1${metricPrefix[i + 1]}`
}