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

Function applyPrecisionHandling

src/helpers.js:173–213  ·  view source on GitHub ↗
(
	value,
	precision,
	e,
	num,
	isDecimal,
	bits,
	ceil,
	roundingFunc,
	round,
	exponent,
)

Source from the content-addressed store, hash-verified

171 * @returns {Object} Object with value and e properties
172 */
173export function applyPrecisionHandling(
174 value,
175 precision,
176 e,
177 num,
178 isDecimal,
179 bits,
180 ceil,
181 roundingFunc,
182 round,
183 exponent,
184) {
185 if (typeof value === "string") {
186 value = parseFloat(value);
187 }
188
189 let result = value.toPrecision(precision);
190
191 const autoExponent = exponent === -1 || isNaN(exponent);
192
193 // Handle scientific notation by recalculating with incremented exponent
194 if (result.includes(E) && e < 8 && autoExponent) {
195 e++;
196 const { result: valueResult } = calculateOptimizedValue(num, e, isDecimal, bits, ceil);
197 let p;
198 if (round > 0) {
199 p = Math.pow(10, round);
200 } else {
201 p = 1;
202 }
203 let computed;
204 if (p === 1) {
205 computed = roundingFunc(valueResult);
206 } else {
207 computed = roundingFunc(valueResult * p) / p;
208 }
209 result = computed.toPrecision(precision);
210 }
211
212 return { value: result, e };
213}
214
215/**
216 * Optimized number formatting with locale, separator, and padding

Callers 2

filesizeFunction · 0.90

Calls 1

calculateOptimizedValueFunction · 0.85

Tested by

no test coverage detected