MCPcopy Create free account
hub / github.com/denoland/std / roundFractionToPrecision

Method roundFractionToPrecision

fmt/printf.ts:687–705  ·  view source on GitHub ↗

* Round fraction to precision * @param fractional * @param precision * @returns tuple of fractional and round

(
    fractional: string,
    precision: number,
  )

Source from the content-addressed store, hash-verified

685 * @returns tuple of fractional and round
686 */
687 roundFractionToPrecision(
688 fractional: string,
689 precision: number,
690 ): [string, boolean] {
691 let round = false;
692 if (fractional.length > precision) {
693 fractional = "1" + fractional; // prepend a 1 in case of leading 0
694 let tmp = parseInt(fractional.slice(0, precision + 2)) / 10;
695 tmp = Math.round(tmp);
696 fractional = Math.floor(tmp).toString();
697 round = fractional[0] === "2";
698 fractional = fractional.slice(1); // remove extra 1
699 } else {
700 while (fractional.length < precision) {
701 fractional += "0";
702 }
703 }
704 return [fractional, round];
705 }
706
707 /**
708 * Format float E

Callers 2

fmtFloatEMethod · 0.95
fmtFloatFMethod · 0.95

Calls 3

floorMethod · 0.80
sliceMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected