-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: refactor, fixes bugs, add test cases for
formatNumber
there was previously a bug where it formats negative 3 digits negative number (e.g -100) to become -'100. This PR fixes this as well as adding tests and moved the function to `utils/rates.ts`
- Loading branch information
1 parent
6400996
commit 071474d
Showing
4 changed files
with
56 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { formatNumber } from './rates'; | ||
|
||
describe('rates utils', () => { | ||
describe('formatNumber', () => { | ||
it('formats positive number without thousand separator', () => { | ||
expect(formatNumber(532.12, 2)).toBe('532.12'); | ||
}); | ||
|
||
it('formats positive number with thousand separator', () => { | ||
expect(formatNumber(1532.12, 2)).toBe('1\'532.12'); | ||
}); | ||
|
||
it('formats negative number without thousand separator', () => { | ||
expect(formatNumber(-532.12, 2)).toBe('-532.12'); | ||
}); | ||
|
||
it('formats negative number with thousand separator', () => { | ||
expect(formatNumber(-1532.12, 2)).toBe('-1\'532.12'); | ||
}); | ||
|
||
it('handles zero correctly', () => { | ||
expect(formatNumber(0, 2)).toBe('0.00'); | ||
}); | ||
|
||
it('formats number with multiple thousand separators', () => { | ||
expect(formatNumber(1234567.89, 2)).toBe('1\'234\'567.89'); | ||
}); | ||
|
||
it('rounds decimal places correctly', () => { | ||
expect(formatNumber(1234.5678, 2)).toBe('1\'234.57'); | ||
}); | ||
|
||
it('formats negative number close to zero without separator', () => { | ||
expect(formatNumber(-100, 2)).toBe('-100.00'); | ||
}); | ||
|
||
it('formats large negative number with separators', () => { | ||
expect(formatNumber(-123456.789, 3)).toBe('-123\'456.789'); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function formatNumber(amount: number, maxDigits: number): string { | ||
let formatted = amount.toFixed(maxDigits); | ||
let position = formatted.indexOf('.') - 3; | ||
const start = formatted[0] === '-' ? 1 : 0; | ||
|
||
while (position > start) { | ||
formatted = formatted.slice(0, position) + '\'' + formatted.slice(position); | ||
position -= 3; | ||
} | ||
return formatted; | ||
} |