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

Add toPercent utility for compatibility

This commit is contained in:
Bill Thornton 2022-10-16 02:41:30 -04:00
parent b008f2db30
commit 9cc6ca66e8
2 changed files with 25 additions and 4 deletions

23
src/utils/number.ts Normal file
View file

@ -0,0 +1,23 @@
function toLocaleStringSupportsOptions() {
return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function');
}
/**
* Gets the value of a number formatted as a perentage.
* @param {number} value The value as a number.
* @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)}%`;
}