29 lines
845 B
TypeScript
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)}`;
|
|
}
|