Skip to content

Commit 3deb655

Browse files
committed
fix(insights): don't show very small numbers as zero
1 parent ca9dbd4 commit 3deb655

File tree

1 file changed

+49
-17
lines changed
  • apps/insights/src/components/LivePrices

1 file changed

+49
-17
lines changed

apps/insights/src/components/LivePrices/index.tsx

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,17 @@ const Price = ({
6666
}: {
6767
prev?: number | undefined;
6868
current?: number | undefined;
69-
}) => {
70-
const numberFormatter = useNumberFormatter({ maximumFractionDigits: 5 });
71-
72-
return current === undefined ? (
69+
}) =>
70+
current === undefined ? (
7371
<Skeleton width={SKELETON_WIDTH} />
7472
) : (
7573
<span
7674
className={styles.price}
7775
data-direction={prev ? getChangeDirection(prev, current) : "flat"}
7876
>
79-
{numberFormatter.format(current)}
77+
<FormattedNumber n={current} />
8078
</span>
8179
);
82-
};
8380

8481
export const LiveConfidence = ({
8582
publisherKey,
@@ -119,21 +116,56 @@ const LiveComponentConfidence = ({
119116
return <Confidence confidence={current?.latest.confidence} />;
120117
};
121118

122-
const Confidence = ({ confidence }: { confidence?: number | undefined }) => {
123-
const numberFormatter = useNumberFormatter({ maximumFractionDigits: 5 });
119+
const Confidence = ({ confidence }: { confidence?: number | undefined }) => (
120+
<span className={styles.confidence}>
121+
<PlusMinus className={styles.plusMinus} />
122+
{confidence === undefined ? (
123+
<Skeleton width={SKELETON_WIDTH} />
124+
) : (
125+
<span>
126+
<FormattedNumber n={confidence} />
127+
</span>
128+
)}
129+
</span>
130+
);
124131

125-
return (
126-
<span className={styles.confidence}>
127-
<PlusMinus className={styles.plusMinus} />
128-
{confidence === undefined ? (
129-
<Skeleton width={SKELETON_WIDTH} />
130-
) : (
131-
<span>{numberFormatter.format(confidence)}</span>
132-
)}
133-
</span>
132+
const FormattedNumber = ({ n }: { n: number }) => {
133+
const bigNumberFormatter = useNumberFormatter({ maximumFractionDigits: 2 });
134+
const smallNumberFormatter = useNumberFormatter({
135+
maximumSignificantDigits: 5,
136+
});
137+
return useMemo(
138+
() =>
139+
n >= 1000
140+
? bigNumberFormatter.format(n)
141+
: formatToSubscriptNumber(smallNumberFormatter.format(n)),
142+
[n, bigNumberFormatter, smallNumberFormatter],
134143
);
135144
};
136145

146+
const formatToSubscriptNumber = (numString: string) => {
147+
const parts = numString.split(".");
148+
149+
const [integerPart, decimalPart] = parts;
150+
if (integerPart && decimalPart) {
151+
const zerosCount =
152+
decimalPart.length - decimalPart.replace(/^0+/, "").length;
153+
154+
return zerosCount < 5
155+
? numString
156+
: integerPart +
157+
"." +
158+
"0" +
159+
(zerosCount > 9
160+
? String.fromCodePoint(0x20_80 + Math.floor(zerosCount / 10))
161+
: "") +
162+
String.fromCodePoint(0x20_80 + (zerosCount % 10)) +
163+
decimalPart.replace(/^0+/, "");
164+
} else {
165+
return numString;
166+
}
167+
};
168+
137169
export const LiveLastUpdated = ({
138170
feedKey,
139171
cluster,

0 commit comments

Comments
 (0)