Add formatCurrency method to shared package as well

This commit is contained in:
Lordmau5 2024-03-14 17:12:59 +01:00
parent 6f6859d309
commit 943d8cdfe6

View File

@ -1,3 +1,8 @@
import { Configschema } from '@esa-layouts/types/schemas';
import { DeepWritable } from 'ts-essentials';
const config = nodecg.bundleConfig;
/**
* Checks if number needs a 0 adding to the start and does so if needed.
* @param num Number which you want to turn into a padded string.
@ -19,10 +24,25 @@ export function msToTimeStr(ms: number): string {
}:${padTimeNumber(seconds)}`;
}
/**
* Simple formatter for displaying currency amounts.
* @param amount Amount as a integer/float.
* @param _symbol The currency symbol (defaults to what's set in the config)
*/
export function formatCurrency(
amount: number,
_symbol?: string,
): string {
const cfg = (config as DeepWritable<Configschema>).event.currency;
const symbol = _symbol || cfg.symbol;
return `${symbol}${amount.toFixed(2)}`;
}
/**
* Simple formatter for displaying USD amounts.
* @param amount Amount as a integer/float.
*/
export function formatUSD(amount: number): string {
return `$${amount.toFixed(2)}`;
return formatCurrency(amount);
}