feat: enhance quantity and price rounding functions for improved precision in trading calculations

This commit is contained in:
discountry
2025-11-06 21:06:03 +08:00
parent 0f70c6b6aa
commit d0d1afa0ea
5 changed files with 92 additions and 18 deletions
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import { roundQtyDownToStep, roundDownToTick, decimalsOf } from "../../src/utils/math";
describe("utils/math precision", () => {
it("keeps quantity intact when it matches the step exactly", () => {
expect(roundQtyDownToStep(0.01, 0.00001)).toBe(0.01);
expect(roundQtyDownToStep(0.01, 0.001)).toBe(0.01);
});
it("floors quantity to the nearest valid step without precision loss", () => {
expect(roundQtyDownToStep(1.23456789, 0.001)).toBe(1.234);
expect(roundQtyDownToStep(0.00009, 0.00005)).toBe(0.00005);
});
it("rounds prices down respecting tick size", () => {
expect(roundDownToTick(20345.123456, 0.001)).toBe(20345.123);
expect(roundDownToTick(1.00000009, 0.00001)).toBe(1);
});
it("detects decimal places for powers of ten", () => {
expect(decimalsOf(0.00000001)).toBe(8);
expect(decimalsOf(0.25)).toBe(2);
expect(decimalsOf(1)).toBe(0);
});
});