esa-nodecg/shared/browser_shared/helpers.ts
2024-03-13 11:26:46 +01:00

29 lines
845 B
TypeScript

/**
* 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.
*/
export function padTimeNumber(num: number): string {
return num.toString().padStart(2, '0');
}
/**
* Converts milliseconds into a time string (HH:MM:SS).
* @param ms Milliseconds you wish to convert.
*/
export function msToTimeStr(ms: number): string {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / (1000 * 60)) % 60);
const hours = Math.floor(ms / (1000 * 60 * 60));
return `${padTimeNumber(hours)
}:${padTimeNumber(minutes)
}:${padTimeNumber(seconds)}`;
}
/**
* Simple formatter for displaying USD amounts.
* @param amount Amount as a integer/float.
*/
export function formatUSD(amount: number): string {
return `$${amount.toFixed(2)}`;
}