Skip to content

support for negative numbers in Decimal from @cosmjs/math  #1897

@stalniy

Description

@stalniy

Description

The Decimal class from @cosmjs/math currently rejects negative values and throws an error when the input contains a "-" sign.
This makes it impossible to represent or work with signed decimal numbers in CosmJS.

🔍 Example

import { Decimal } from "@cosmjs/math";

Decimal.fromUserInput("-0.123", 3);
// ❌ Error: Negative values not supported

💡 Expected behavior

Decimal should support negative values, or at least provide an alternative (e.g. SignedDecimal) for cases where signed arithmetic is required.

🧠 Context

In the Go Cosmos SDK, the equivalent type sdk.Dec does support negative numbers.
This is useful for many valid scenarios, such as:

  • Calculating deltas or differences between balances or prices
  • Expressing signed ratios or deviations
  • Performing off-chain accounting or simulation logic

In CosmJS, however, the current implementation appears to be limited to non-negative quantities (like tokens, gas prices, etc.), which restricts flexibility in analytical or off-chain use cases.

🚀 Proposal

  • Extend the existing Decimal class to allow negative values, or
  • Introduce a new SignedDecimal type that:
    • Keeps the same precision handling
    • Encodes/decodes safely
    • Maintains backward compatibility with positive-only decimals

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions