1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/utils/number.ts

35 lines
1 KiB
TypeScript
Raw Normal View History

function toLocaleStringSupportsOptions() {
return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function');
}
2023-03-14 01:32:36 -04:00
/**
* Generates a random integer in a given range.
* @param {number} min - Minimum of the range.
* @param {number} max - Maximum of the range.
* @returns {number} Randomly generated number.
*/
export function randomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Gets the value of a number formatted as a perentage.
* @param {number} value The value as a number.
2022-10-17 01:09:37 -04:00
* @param {string} locale The locale to use for formatting (i.e. en-us).
* @returns {string} The value formatted as a percentage.
*/
export function toPercent(value: number | null | undefined, locale: string): string {
if (value == null) {
return '';
}
if (toLocaleStringSupportsOptions()) {
return value.toLocaleString(locale, {
style: 'percent',
maximumFractionDigits: 0
});
}
return `${Math.round(value * 100)}%`;
}