MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / significantDigits

Function significantDigits

test/big-decimal/cross-validation.test.ts:20–38  ·  view source on GitHub ↗

* Extract significant digits from a numeric string. * Strips sign, decimal point, leading zeros, and trailing zeros. * Returns only the significant digit sequence.

(s: string)

Source from the content-addressed store, hash-verified

18 * Returns only the significant digit sequence.
19 */
20function significantDigits(s: string): string {
21 // Remove sign
22 if (s.startsWith('-') || s.startsWith('+')) s = s.slice(1);
23
24 // Handle scientific notation: normalize to plain digits
25 const eIdx = s.search(/[eE]/);
26 if (eIdx !== -1) {
27 // Extract mantissa digits (ignore exponent for digit comparison)
28 s = s.slice(0, eIdx);
29 }
30
31 // Remove decimal point
32 s = s.replace('.', '');
33
34 // Remove leading zeros
35 s = s.replace(/^0+/, '');
36
37 return s;
38}
39
40/**
41 * Compare the first `matchDigits` significant digits of two numeric strings.

Callers 2

compareDigitsFunction · 0.85
diffMessageFunction · 0.85

Calls 2

sliceMethod · 0.65
replaceMethod · 0.65

Tested by

no test coverage detected