Add formatPriceCurrency

This commit is contained in:
Matthias 2021-12-30 09:38:43 +01:00
parent ac948fccee
commit d7ed2e46c8
2 changed files with 18 additions and 1 deletions

View File

@ -20,6 +20,17 @@ export function formatPrice(value: number, decimals = 8): string {
return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : ''; return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : '';
} }
/**
* Formats price in the format "<price> <StakeCurrency>" 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 { export function dateFromString(datestring: string, format: string): Date {
return parse(datestring, format, 0); return parse(datestring, format, 0);
} }

View File

@ -1,7 +1,13 @@
import { formatPercent } from '@/shared/formatters'; import { formatPercent, formatPriceCurrency } from '@/shared/formatters';
describe('formatters.ts', () => { describe('formatters.ts', () => {
it('Format percent correctly', () => { it('Format percent correctly', () => {
expect(formatPercent(0.5)).toEqual('50.000%'); 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');
});
}); });