Fix calculation
This commit is contained in:
@@ -18,13 +18,18 @@ export const formatCount = (i18n: I18n, num: number) => {
|
|||||||
// HACK: janky workaround for formatjs not supporting roundingMode
|
// HACK: janky workaround for formatjs not supporting roundingMode
|
||||||
// https://github.com/formatjs/formatjs/issues/4359
|
// https://github.com/formatjs/formatjs/issues/4359
|
||||||
// TODO: remove when fixed by formatjs -sfn
|
// TODO: remove when fixed by formatjs -sfn
|
||||||
//
|
const factor =
|
||||||
// error is when the number is within 50 of the threshold to the next rounded value
|
num < 1e3
|
||||||
// eg. 1050 is within 50 of 1100, so will incorrectly round to 1.1k. 1049 correctly goes to 1k
|
? 1
|
||||||
const isNearThreshould = num > 1000 && num % 100 >= 50
|
: num < 1e6
|
||||||
// applying `-((num % 100) - 49)` to the number will take it back down to beneath the threshold
|
? 1e2 // 1e(6-4)
|
||||||
const correctedNum = isNearThreshould ? num - ((num % 100) - 49) : num
|
: num < 1e9
|
||||||
return i18n.number(correctedNum, {
|
? 1e5 // 1e(9-4)
|
||||||
|
: num < 1e12
|
||||||
|
? 1e8 // 1e(12-4)
|
||||||
|
: 1e11 // 1e(15-4)
|
||||||
|
const truncatedNum = Math.trunc(num / factor) * factor
|
||||||
|
return i18n.number(truncatedNum, {
|
||||||
notation: 'compact',
|
notation: 'compact',
|
||||||
maximumFractionDigits: 1,
|
maximumFractionDigits: 1,
|
||||||
// @ts-ignore types are missing in CI
|
// @ts-ignore types are missing in CI
|
||||||
|
|||||||
@@ -53,6 +53,9 @@ export function NumberFormat() {
|
|||||||
<Text>
|
<Text>
|
||||||
10950 <ArrowIcon size="xs" /> {formatCount(i18n, 10950)}
|
10950 <ArrowIcon size="xs" /> {formatCount(i18n, 10950)}
|
||||||
</Text>
|
</Text>
|
||||||
|
<Text>
|
||||||
|
10999 <ArrowIcon size="xs" /> {formatCount(i18n, 10999)}
|
||||||
|
</Text>
|
||||||
<Text>
|
<Text>
|
||||||
109950 <ArrowIcon size="xs" /> {formatCount(i18n, 109950)}
|
109950 <ArrowIcon size="xs" /> {formatCount(i18n, 109950)}
|
||||||
</Text>
|
</Text>
|
||||||
|
|||||||
Reference in New Issue
Block a user