Skip to content

Commit

Permalink
prevent scientific display in inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
GrandSchtroumpf committed Sep 3, 2024
1 parent 1c13fd2 commit a51949e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/utils/helpers/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ describe('Test helpers', () => {
test('formatNumber', () => {
expect(formatNumber('')).toBe('');
expect(formatNumber('.')).toBe('0');
expect(formatNumber('0.0000000')).toBe('0');
expect(formatNumber('.1')).toBe('0.1');
expect(formatNumber('1.1010')).toBe('1.101');
expect(formatNumber('01.100')).toBe('1.1');
expect(formatNumber('01.00')).toBe('1');
expect(formatNumber('1.000000000100')).toBe('1.0000000001');
expect(formatNumber('.000000000100')).toBe('0.0000000001');
});
describe('prettifyNumber', () => {
test('should return 0 for input lower then 0', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/utils/helpers/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const sanitizeNumber = (input: string, precision?: number): string => {
export const formatNumber = (value: string) => {
if (!value) return '';
if (value === '.') return '0';
return new SafeDecimal(value).toString();
return new SafeDecimal(value)
.toFixed(100)
.replace(lastZero, '')
.replace(/\.+$/, ''); // Remove "." if it's the last character
};

const getDisplayCurrency = (currency: string) => {
Expand Down Expand Up @@ -219,7 +222,7 @@ export const roundSearchParam = (param?: string | number) => {
maxDecimals += decimals.match(firstZero)?.[0].length ?? 0;
}
const roundedDecimals = decimals.slice(0, maxDecimals).replace(lastZero, '');
return [radix, roundedDecimals].filter((v) => !!v).join('.');
return [radix || '0', roundedDecimals].filter((v) => !!v).join('.');
};

export const tokenAmount = (
Expand Down

0 comments on commit a51949e

Please sign in to comment.