MCPcopy Create free account
hub / github.com/avoidwork/filesize.js / applyNumberFormatting

Function applyNumberFormatting

src/helpers.js:225–270  ·  view source on GitHub ↗
(
	value,
	locale,
	localeOptions,
	separator,
	pad,
	round,
	roundingFunc,
)

Source from the content-addressed store, hash-verified

223 * @returns {string|number} Formatted value
224 */
225export function applyNumberFormatting(
226 value,
227 locale,
228 localeOptions,
229 separator,
230 pad,
231 round,
232 roundingFunc,
233) {
234 let result = value;
235
236 // When padding alongside a locale, let the locale formatter emit the fixed
237 // number of fraction digits. The manual string padding below cannot tell a
238 // locale-inserted grouping separator from the decimal separator, so it
239 // dropped digits (e.g. "1,234,500" became "1,234").
240 const localePad =
241 pad && round > 0 ? { minimumFractionDigits: round, maximumFractionDigits: round } : undefined;
242
243 // Apply locale formatting
244 if (locale === true) {
245 result = result.toLocaleString(undefined, localePad);
246 } else if (locale.length > 0) {
247 result = result.toLocaleString(locale, { ...localeOptions, ...localePad });
248 } else if (separator.length > 0) {
249 // Round before separator replacement to ensure excess decimal places
250 // are truncated when pad is also set (fixes padding + separator bug).
251 if (pad && round > 0) {
252 const p = Math.pow(10, round);
253 result = roundingFunc(result * p) / p;
254 }
255 result = result.toString().replace(PERIOD, separator);
256 }
257
258 // Apply padding for the non-locale paths, where the string has a single
259 // decimal separator and no grouping is inserted.
260 if (pad && round > 0 && locale !== true && locale.length === 0) {
261 const resultStr = result.toString();
262 const x = separator || PERIOD;
263 const tmp = resultStr.split(x);
264 const s = tmp[1] || EMPTY;
265
266 result = `${tmp[0]}${x}${s.padEnd(round, ZERO)}`;
267 }
268
269 return result;
270}
271
272/**
273 * Calculates exponent from the input value using pre-computed log values and clamps to supported range

Callers 2

decorateResultFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected