diff --git a/src/shared/formatters.ts b/src/shared/formatters.ts index 1b6c4aea..e140b7b4 100644 --- a/src/shared/formatters.ts +++ b/src/shared/formatters.ts @@ -20,6 +20,17 @@ export function formatPrice(value: number, decimals = 8): string { return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : ''; } +/** + * Formats price in the format " " using "deciaml" decimals + * @param price Price to format + * @param currency currency to use + * @param decimals Decimals + * @returns + */ +export function formatPriceCurrency(price, currency: string, decimals = 3) { + return `${formatPrice(price, decimals)} ${currency}`; +} + export function dateFromString(datestring: string, format: string): Date { return parse(datestring, format, 0); } diff --git a/tests/unit/formatters.spec.ts b/tests/unit/formatters.spec.ts index 97192581..ceb6e70c 100644 --- a/tests/unit/formatters.spec.ts +++ b/tests/unit/formatters.spec.ts @@ -1,7 +1,13 @@ -import { formatPercent } from '@/shared/formatters'; +import { formatPercent, formatPriceCurrency } from '@/shared/formatters'; describe('formatters.ts', () => { it('Format percent correctly', () => { expect(formatPercent(0.5)).toEqual('50.000%'); }); + + it('format price currency as expected', () => { + expect(formatPriceCurrency(5123.551123, 'USDT', 3)).toEqual('5123.551 USDT'); + expect(formatPriceCurrency(5123.551123, 'USDT')).toEqual('5123.551 USDT'); + expect(formatPriceCurrency(5123.551123, 'USDT', 5)).toEqual('5123.55112 USDT'); + }); });