UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

51 lines 1.37 kB
import { ABSENT_VALUE_FORMAT, isAbsent, buildReturn, cleanNumber, formatDecimals, formatNumberCore, prepareFormatOptions, resolveLocale } from "./formatCore.js"; import { countDecimals } from "./decimals.js"; export function formatPercent(value, { locale: inputLocale = null, clean = false, decimals = null, rounding = null, signDisplay = null, options = null, returnAria = false, invalidAriaText = null, cleanCopyValue = null } = {}) { value = isAbsent(value) ? ABSENT_VALUE_FORMAT : value; const locale = resolveLocale(inputLocale); const opts = prepareFormatOptions({ options, signDisplay }); if (clean) { value = cleanNumber(value); } if (parseFloat(String(decimals)) >= 0) { value = formatDecimals(value, decimals, rounding, opts); } if (decimals === null) { if (typeof opts.maximumFractionDigits === 'undefined') { decimals = countDecimals(value); } value = formatDecimals(value, decimals, rounding, opts); } if (!opts.style) { opts.style = 'percent'; } const display = formatNumberCore(Number(value) / 100, locale, opts); const aria = display; if (!returnAria) { return display; } return buildReturn({ value, locale, display, aria, type: 'number', opts, cleanCopyValue, invalidAriaText }); } //# sourceMappingURL=formatPercent.js.map