Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nig-718 #125

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/NumberDisplayer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ it('if integer part less than 1, decimal part should have 4 decimals', () => {
it('if decimal part less than 0.0001, decimal part should round into 4 decimals', () => {
expect(render(<NumberDisplayer text="000000000012455" />).container).toMatchSnapshot();
});
it('if decimal part less than 0.0001, decimal part should round into 3 decimals', () => {
expect(
render(<NumberDisplayer text="000000000012455" longZeroCasePrecision={3} />).container
).toMatchSnapshot();
});

it('should throw error if text is not string', () => {
expect(() =>
Expand Down
18 changes: 15 additions & 3 deletions src/components/NumberDisplayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ type NumberDisplayerProps = {
* @default false
*/
loading?: boolean;
/**
* 有很多个零的情况下最后数字的精度
* @default 4
*/
longZeroCasePrecision?: number;
};

/**
* 展示 bignumber 组件
*
* eg. 0.0{4}5252
*/
export function NumberDisplayer({ text = '0', className, loading = false }: NumberDisplayerProps) {
export function NumberDisplayer({
text = '0',
className,
loading = false,
longZeroCasePrecision = 4,
}: NumberDisplayerProps) {
// 防止不会使用,或者错误传错类型,有助于开发阶段尽早发现问题
if (typeof text !== 'string') throw new Error('text should be string');
// 转成处理过后的字符串形式
Expand Down Expand Up @@ -46,11 +56,13 @@ export function NumberDisplayer({ text = '0', className, loading = false }: Numb
const lastZeroIndex = getLastZero(valueAfterDot);
const zeroCount = valueAfterDot.substring(0, lastZeroIndex + 1).length;
const noneZeroValue = valueAfterDot.substring(lastZeroIndex + 1);
const [, noneZeroRealValue] = BigNumber(`0.${noneZeroValue}`).toFixed(4).split('.');
const [, noneZeroRealValue] = BigNumber(`0.${noneZeroValue}`)
.toFixed(longZeroCasePrecision)
.split('.');
value = `0{${zeroCount}}${noneZeroRealValue}`;
}
return value;
}, [valueAfterDot, valueBeforeDot]);
}, [longZeroCasePrecision, valueAfterDot, valueBeforeDot]);
// 拆分成2部分

return (
Expand Down
8 changes: 8 additions & 0 deletions src/components/__snapshots__/NumberDisplayer.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ exports[`bignumber should work well 1`] = `
</div>
`;

exports[`if decimal part less than 0.0001, decimal part should round into 3 decimals 1`] = `
<div>
<span>
0.0{13}125
</span>
</div>
`;

exports[`if decimal part less than 0.0001, decimal part should round into 4 decimals 1`] = `
<div>
<span>
Expand Down
2 changes: 1 addition & 1 deletion src/components/twitterAdded/feedsPage/ethIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const FriendPrice: FC<FriendPriceProps> = ({ twitterUsername }) => {
</defs>
</svg>
<p className="mx-auto text-[12px] font-medium">
<NumberDisplayer text={userInfo?.price} />
<NumberDisplayer text={userInfo?.price} longZeroCasePrecision={3} />
</p>
</div>
) : null;
Expand Down
Loading