| 14 | * @returns {string} |
| 15 | */ |
| 16 | export function formatNumber(number, options = /** @type {FormatOptions} */ ({})) { |
| 17 | const localizeManager = getLocalizeManager(); |
| 18 | if (number === undefined || number === null) return ''; |
| 19 | const formattedToParts = formatNumberToParts(number, options); |
| 20 | // If number is not a number |
| 21 | if ( |
| 22 | formattedToParts === options.returnIfNaN || |
| 23 | formattedToParts === localizeManager.formatNumberOptions.returnIfNaN |
| 24 | ) { |
| 25 | return /** @type {string} */ (formattedToParts); |
| 26 | } |
| 27 | let printNumberOfParts = ''; |
| 28 | // update numberOfParts because there may be some parts added |
| 29 | const numberOfParts = formattedToParts ? formattedToParts.length : 0; |
| 30 | for (let i = 0; i < numberOfParts; i += 1) { |
| 31 | const part = /** @type {FormatNumberPart} */ (formattedToParts[i]); |
| 32 | printNumberOfParts += part.value; |
| 33 | } |
| 34 | |
| 35 | const computedLocale = getLocale(options && options.locale); |
| 36 | |
| 37 | if (localizeManager.formatNumberOptions.postProcessors.size > 0) { |
| 38 | Array.from(localizeManager.formatNumberOptions.postProcessors).forEach(([locale, fn]) => { |
| 39 | if (locale === computedLocale) { |
| 40 | printNumberOfParts = fn(printNumberOfParts); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | |
| 45 | if (options.postProcessors && options.postProcessors.size > 0) { |
| 46 | Array.from(options.postProcessors).forEach(([locale, fn]) => { |
| 47 | if (locale === computedLocale) { |
| 48 | printNumberOfParts = fn(printNumberOfParts); |
| 49 | } |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | return printNumberOfParts; |
| 54 | } |