Skip to content

fix(sdk): correct BigNum zero-precision formatting edge cases#2201

Open
jenish-25 wants to merge 1 commit into
velocity-exchange:masterfrom
jenish-25:fix/bignum-zero-precision-formatting
Open

fix(sdk): correct BigNum zero-precision formatting edge cases#2201
jenish-25 wants to merge 1 commit into
velocity-exchange:masterfrom
jenish-25:fix/bignum-zero-precision-formatting

Conversation

@jenish-25

@jenish-25 jenish-25 commented Jul 5, 2026

Copy link
Copy Markdown

Summary

Fixes two formatting bugs in BigNum (sdk/src/factory/bigNum.ts) that surface when a zero argument is passed.

1. toFixed(0) leaves a dangling decimal delimiter

toFixed always appends the delimiter + fractional part, so zero precision produced a malformed string:

BigNum.from(123, 0).toFixed(0) // "123."  (expected "123")
BigNum.from(5, 1).toFixed(0)   // "0."    (expected "0")
BigNum.from(-5, 0).toFixed(0)  // "-5."   (expected "-5")

Fix: when precision is zero there is no fractional part, so return the integer portion directly.

2. toNotional() ignores a decimalOverride of 0

The usingCustomPrecision check folded decimalOverride into a truthiness test, so 0 (a legitimate value meaning "whole-dollar formatting") was treated as "not provided" and fell back to the 2-decimal default:

const val = BigNum.fromPrint('1234.56', new BN(6));
val.toNotional(undefined, undefined, 0) // "$1,234.56"  (expected "$1,234")
val.toNotional(undefined, undefined, 3) // "$1,234.560" (already worked)

Fix: test decimalOverride !== undefined instead of truthiness. prettyPrint already handles 0 correctly downstream.

Tests

Added two cases to sdk/tests/bn/test.ts covering both fixes (and asserting the non-zero / default paths are unchanged). All BigNum tests pass, and prettier --check is clean.

Summary by CodeRabbit

  • Bug Fixes
    • Improved number formatting for zero or negative decimal precision so values no longer show an unnecessary trailing decimal separator.
    • Fixed notional formatting to correctly treat a 0 decimal override as an intentional setting, preserving expected whole-number output.
  • Tests
    • Added coverage for formatting edge cases, including zero precision, negative values, and zero decimal overrides.

Two formatting bugs in BigNum surfaced with a zero argument:

- toFixed(0) returned a value with a dangling decimal delimiter
  ('123.', '0.', '-5.') instead of the integer portion ('123', '0',
  '-5'). Return the left side directly when precision is zero.

- toNotional() ignored a decimalOverride of 0 because the value was
  folded into a truthiness check, so whole-dollar formatting fell back
  to the 2-decimal default. Test for presence (!== undefined) instead.

Adds unit tests for both cases to tests/bn/test.ts.
@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ade8fc95-5ea2-4de2-ba92-b8a4852a7f1d

📥 Commits

Reviewing files that changed from the base of the PR and between 13e8e9b and 31b38dd.

📒 Files selected for processing (2)
  • sdk/src/factory/bigNum.ts
  • sdk/tests/bn/test.ts

Walkthrough

Modified BigNum.toFixed to return only the integer portion when fixedPrecision is zero or negative, and updated BigNum.toNotional to treat a decimalOverride of 0 as an explicit custom precision value rather than falsy. Added corresponding unit tests validating both edge cases.

Changes

BigNum Precision Handling

Layer / File(s) Summary
Zero-precision formatting logic
sdk/src/factory/bigNum.ts
toFixed returns only the integer side when precision is <= 0; toNotional now checks decimalOverride !== undefined so a 0 override is treated as custom precision.
Edge-case unit tests
sdk/tests/bn/test.ts
New tests verify toFixed(0) omits the decimal delimiter with correct truncation, and toNotional with decimalOverride=0 drops the fractional part while other overrides remain unaffected.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Poem

A zero came hopping, precise and small,
No trailing dot, no decimal at all!
The notional too learned zero's true worth,
Not falsy, but real—since its very birth.
Tests confirm it, snug and neat—
This bunny's math is now complete! 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main BigNum zero-precision formatting fixes covered by the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant