2022-10-16 02:41:30 -04:00
|
|
|
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.
|
2022-10-17 01:09:37 -04:00
|
|
|
* @param {string} locale The locale to use for formatting (i.e. en-us).
|
2022-10-16 02:41:30 -04:00
|
|
|
* @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)}%`;
|
|
|
|
}
|