MCPcopy
hub / github.com/colinhacks/zod / floatSafeRemainder

Function floatSafeRemainder

packages/zod/src/v3/types.ts:1354–1361  ·  view source on GitHub ↗
(val: number, step: number)

Source from the content-addressed store, hash-verified

1352
1353// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
1354function floatSafeRemainder(val: number, step: number) {
1355 const valDecCount = (val.toString().split(".")[1] || "").length;
1356 const stepDecCount = (step.toString().split(".")[1] || "").length;
1357 const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
1358 const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
1359 const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
1360 return (valInt % stepInt) / 10 ** decCount;
1361}
1362
1363export interface ZodNumberDef extends ZodTypeDef {
1364 checks: ZodNumberCheck[];

Callers 1

_parseMethod · 0.70

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected