Fix calculation

This commit is contained in:
Dan Abramov
2024-11-06 04:34:41 +00:00
parent f9994b2b42
commit 16dac5dfd4
2 changed files with 15 additions and 7 deletions
+12 -7
View File
@@ -18,13 +18,18 @@ export const formatCount = (i18n: I18n, num: number) => {
// HACK: janky workaround for formatjs not supporting roundingMode
// https://github.com/formatjs/formatjs/issues/4359
// TODO: remove when fixed by formatjs -sfn
//
// error is when the number is within 50 of the threshold to the next rounded value
// eg. 1050 is within 50 of 1100, so will incorrectly round to 1.1k. 1049 correctly goes to 1k
const isNearThreshould = num > 1000 && num % 100 >= 50
// applying `-((num % 100) - 49)` to the number will take it back down to beneath the threshold
const correctedNum = isNearThreshould ? num - ((num % 100) - 49) : num
return i18n.number(correctedNum, {
const factor =
num < 1e3
? 1
: num < 1e6
? 1e2 // 1e(6-4)
: num < 1e9
? 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',
maximumFractionDigits: 1,
// @ts-ignore types are missing in CI
@@ -53,6 +53,9 @@ export function NumberFormat() {
<Text>
10950 <ArrowIcon size="xs" /> {formatCount(i18n, 10950)}
</Text>
<Text>
10999 <ArrowIcon size="xs" /> {formatCount(i18n, 10999)}
</Text>
<Text>
109950 <ArrowIcon size="xs" /> {formatCount(i18n, 109950)}
</Text>