MCPcopy Create free account
hub / github.com/apache/fory / formatParts

Function formatParts

javascript/packages/core/lib/compatible/scalar.ts:422–444  ·  view source on GitHub ↗
(value: DecimalParts, forceDecimal: boolean)

Source from the content-addressed store, hash-verified

420}
421
422function formatParts(value: DecimalParts, forceDecimal: boolean): string {
423 const normalized = normalizeParts(value);
424 if (normalized.unscaled === 0n) {
425 if (normalized.negativeZero) {
426 return forceDecimal ? "-0.0" : "-0";
427 }
428 return forceDecimal ? "0.0" : "0";
429 }
430 const negative = normalized.unscaled < 0n;
431 const digits = (
432 negative ? -normalized.unscaled : normalized.unscaled
433 ).toString();
434 let result: string;
435 if (normalized.scale === 0) {
436 result = forceDecimal ? `${digits}.0` : digits;
437 } else if (digits.length > normalized.scale) {
438 const split = digits.length - normalized.scale;
439 result = `${digits.slice(0, split)}.${digits.slice(split)}`;
440 } else {
441 result = `0.${"0".repeat(normalized.scale - digits.length)}${digits}`;
442 }
443 return negative ? `-${result}` : result;
444}
445
446function partsEqual(left: DecimalParts, right: DecimalParts): boolean {
447 const l = normalizeParts(left);

Callers 3

partsToNumberFunction · 0.85
floatToStringMethod · 0.85
decimalToStringMethod · 0.85

Calls 4

normalizePartsFunction · 0.85
toStringMethod · 0.65
sliceMethod · 0.45
repeatMethod · 0.45

Tested by

no test coverage detected