Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/utils/_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export function formatPrice(price: string | number, szDecimals: number, type: "p
const maxDecimals = Math.max((type === "perp" ? 6 : 8) - szDecimals, 0);
price = StringMath.toFixedTruncate(price, maxDecimals);

// Integer prices are always allowed, including values that become integers
// after decimal truncation (e.g. "100001.0" -> "100001"). Without this
// re-check the significant-figure step below would truncate them (-> "100000").
// A value that truncated all the way to zero is left for the zero check below.
if (/^-?\d+$/.test(price) && !/^-?0+$/.test(price)) {
return formatDecimalString(price);
}

// Apply sig figs limit: max 5 significant figures
price = StringMath.toPrecisionTruncate(price, 5);

Expand Down
8 changes: 8 additions & 0 deletions tests/utils/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ Deno.test("formatPrice", async (t) => {
assertEquals(formatPrice("1234567", 0), "1234567");
});

await t.step("integer value with trailing decimal zeros bypasses limit", () => {
assertEquals(formatPrice("100001.0", 0), "100001");
assertEquals(formatPrice("123456.0", 0), "123456");
assertEquals(formatPrice("100001.00", 0), "100001");
assertEquals(formatPrice("-123456.0", 0), "-123456");
assertEquals(formatPrice("100001.0", 0, "spot"), "100001");
});

await t.step("truncates to 5 sig figs", () => {
assertEquals(formatPrice("12345.6", 0), "12345");
assertEquals(formatPrice("0.00123456", 0), "0.001234");
Expand Down