Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • migrate to decimaljs from number.parseFloat, for precision

Type of Change

  • New feature

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Dec 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Dec 26, 2025 8:30pm

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 26, 2025

Greptile Summary

This PR migrates billing calculations from Number.parseFloat to decimal.js for improved floating-point precision in financial operations. The change introduces a new utility module (decimal.ts) with helper functions and updates all billing-related calculations across overage computation, usage tracking, and credit balance operations.

Key changes:

  • Created centralized decimal utility with toDecimal(), toNumber(), and toFixedString() helpers
  • Replaced all Number.parseFloat() calls with toDecimal() for proper null/undefined handling
  • Updated arithmetic operations to use Decimal methods (.plus(), .minus(), .lessThan(), etc.)
  • Maintained proper conversion back to numbers at API boundaries using toNumber()
  • SQL queries now use toFixedString() for consistent decimal string formatting

Implementation pattern:
The code follows a consistent pattern of: parse DB values → perform calculations with Decimal → convert back to number for responses. This ensures precision is maintained during intermediate calculations while keeping API contracts unchanged.

Confidence Score: 4/5

  • This PR is safe to merge with minor considerations around global configuration
  • The migration to decimal.js is well-executed with proper null handling and consistent patterns. The code correctly replaces Number.parseFloat with toDecimal() and maintains precision throughout calculations. However, the global Decimal.set() configuration could theoretically affect other parts of the application if decimal.js is used elsewhere in the future
  • Pay attention to apps/sim/lib/billing/utils/decimal.ts for the global configuration pattern

Important Files Changed

Filename Overview
apps/sim/lib/billing/utils/decimal.ts New utility module for Decimal.js operations with helper functions for billing precision
apps/sim/lib/billing/core/billing.ts Migrated overage calculations and usage summations from Number.parseFloat to Decimal.js
apps/sim/lib/billing/core/usage.ts Converted all usage limit and cost calculations to use Decimal for precision
apps/sim/lib/billing/credits/balance.ts Updated credit balance operations and atomic deduction logic to use Decimal

Sequence Diagram

sequenceDiagram
    participant Client
    participant BillingAPI
    participant Decimal Utils
    participant Database
    
    Client->>BillingAPI: Request billing summary
    BillingAPI->>Database: Fetch user/org usage data
    Database-->>BillingAPI: Return decimal strings
    BillingAPI->>Decimal Utils: toDecimal(value)
    Decimal Utils-->>BillingAPI: Decimal object
    BillingAPI->>Decimal Utils: Perform calculations (plus, minus, max)
    Decimal Utils-->>BillingAPI: Decimal results
    BillingAPI->>Decimal Utils: toNumber(decimal)
    Decimal Utils-->>BillingAPI: Number for response
    BillingAPI-->>Client: Return billing summary
    
    Client->>BillingAPI: Deduct credits
    BillingAPI->>Decimal Utils: toDecimal(cost)
    Decimal Utils-->>BillingAPI: Decimal object
    BillingAPI->>Decimal Utils: toFixedString(cost)
    Decimal Utils-->>BillingAPI: String for SQL
    BillingAPI->>Database: Execute SQL with decimal string
    Database-->>BillingAPI: Return old/new balance
    BillingAPI->>Decimal Utils: toDecimal(oldBalance)
    Decimal Utils-->>BillingAPI: Process comparison
    BillingAPI->>Decimal Utils: toNumber(result)
    Decimal Utils-->>BillingAPI: Final result
    BillingAPI-->>Client: Return deduction result
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Comments (1)

  1. apps/sim/lib/billing/core/billing.ts, line 70 (link)

    logic: inconsistent with decimal.js migration - uses Math.max on regular numbers instead of maintaining decimal precision

5 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Comments (1)

  1. apps/sim/lib/billing/credits/balance.ts, line 165 (link)

    style: inconsistent precision handling - cost and creditsUsed are already numbers (converted from Decimal earlier), but this calculation should ideally use Decimal throughout to avoid potential precision loss before the final conversion

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 88cda3a into staging Dec 26, 2025
11 checks passed
@waleedlatif1 waleedlatif1 deleted the sim-522 branch December 26, 2025 20:35
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.

2 participants