Skip to content

Show negative amounts with a minus sign - #27

Open
sergical wants to merge 1 commit into
releasefrom
fix/currency-negative-amounts
Open

sergical wants to merge 1 commit into
releasefrom
fix/currency-negative-amounts

Conversation

@sergical

Copy link
Copy Markdown
Member

formatUsd now puts a minus sign in front of the dollar sign for negative amounts, so refunds read as -$5 instead of $-5.

Created with Claude Code

Co-authored-by: Claude <claude@anthropic.com>
@github-actions

Copy link
Copy Markdown

Verdict: Needs work — the fix for negative values is correct but the function still produces incorrectly-formatted currency strings (pre-existing bug, not addressed).

Correctness

  • No related automated tests exist (run_tests found 0 tests), so this change has no test coverage.
  • Core pre-existing bug remains unfixed: the function never rounds/pads to two decimal places. formatUsd(100) → "$1" instead of "$1.00"; formatUsd(150) → "$1.5" instead of "$1.50"; formatUsd(5) → "$0.05" works by luck, but formatUsd(1005) can produce floating-point artifacts. This directly undermines the purpose of a currency formatter and should be fixed in the same PR (e.g., via .toFixed(2) or Intl.NumberFormat).
  • No thousands separators for large amounts (e.g. "$1234567.89" instead of "$1,234,567.89").
  • Unhandled inputs: NaN → "$NaN", Infinity/-Infinity → "$Infinity"/-$Infinity". Minor, but worth a guard or documented assumption.
  • The new sign logic itself is correct: Math.abs(cents) < 0 short-circuits properly, -0 is handled sensibly (prints "$0"), and negative values now display with a leading - rather than the old buggy behavior (e.g., old code gave "$-1" style results from -100/100). This part of the diff is a genuine, well-targeted improvement.

Style

  • Prefer a template literal over + concatenation for readability: `${sign}$${Math.abs(cents) / 100}`.
  • Since proper decimal formatting is needed anyway (see correctness above), consider replacing the whole function body with Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(cents / 100), which would also subsume the new sign-handling logic and be more maintainable.
  • sign is an adequate name; not a blocker.

Summary

The negative-number sign fix is correct and well-scoped, but the PR leaves significant pre-existing formatting correctness gaps (no decimal padding/rounding, no grouping) that make the function unreliable as a currency formatter. Recommend addressing the decimal-formatting issue before merging, or explicitly scoping this PR to "sign only" and filing a follow-up for formatting precision.

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