New field currency (#4338)

Closes #4122 
---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
Anoop P
2024-04-02 18:59:57 +05:30
committed by GitHub
parent d694ab1b16
commit bbffde1ca0
12 changed files with 145 additions and 75 deletions

View File

@ -1,28 +1,28 @@
import { amountFormat } from '../amountFormat';
import { formatAmount } from '../formatAmount';
describe('amountFormat', () => {
it('formats numbers less than 1000 correctly', () => {
expect(amountFormat(500)).toBe('500');
expect(amountFormat(123.456)).toBe('123.5');
expect(formatAmount(500)).toBe('500');
expect(formatAmount(123.456)).toBe('123.5');
});
it('formats numbers between 1000 and 999999 correctly', () => {
expect(amountFormat(1500)).toBe('1.5k');
expect(amountFormat(789456)).toBe('789.5k');
expect(formatAmount(1500)).toBe('1.5k');
expect(formatAmount(789456)).toBe('789.5k');
});
it('formats numbers between 1000000 and 999999999 correctly', () => {
expect(amountFormat(2000000)).toBe('2m');
expect(amountFormat(654987654)).toBe('655m');
expect(formatAmount(2000000)).toBe('2m');
expect(formatAmount(654987654)).toBe('655m');
});
it('formats numbers greater than or equal to 1000000000 correctly', () => {
expect(amountFormat(1200000000)).toBe('1.2b');
expect(amountFormat(987654321987)).toBe('987.7b');
expect(formatAmount(1200000000)).toBe('1.2b');
expect(formatAmount(987654321987)).toBe('987.7b');
});
it('handles numbers with decimal places correctly', () => {
expect(amountFormat(123.456)).toBe('123.5');
expect(amountFormat(789.0123)).toBe('789');
expect(formatAmount(123.456)).toBe('123.5');
expect(formatAmount(789.0123)).toBe('789');
});
});

View File

@ -1,11 +0,0 @@
export const amountFormat = (number: number) => {
if (number < 1000) {
return number.toFixed(1).replace(/\.?0+$/, '');
} else if (number < 1000000) {
return (number / 1000).toFixed(1).replace(/\.?0+$/, '') + 'k';
} else if (number < 1000000000) {
return (number / 1000000).toFixed(1).replace(/\.?0+$/, '') + 'm';
} else {
return (number / 1000000000).toFixed(1).replace(/\.?0+$/, '') + 'b';
}
};

View File

@ -0,0 +1,11 @@
export const formatAmount = (amount: number) => {
if (amount < 1000) {
return amount.toFixed(1).replace(/\.?0+$/, '');
} else if (amount < 1000000) {
return (amount / 1000).toFixed(1).replace(/\.?0+$/, '') + 'k';
} else if (amount < 1000000000) {
return (amount / 1000000).toFixed(1).replace(/\.?0+$/, '') + 'm';
} else {
return (amount / 1000000000).toFixed(1).replace(/\.?0+$/, '') + 'b';
}
};