Support decimal values and arithmetic · Issue #2872 · microsoft/TypeScript · GitHub
Skip to content

Support decimal values and arithmetic #2872

Description

TypeScript should support decimal values and arithmetic, similar to .NET's Decimal and/or Java's BigDecimal type. This would solve some serious problems when writing business applications in JavaScript.

For the following TS code:

var a = 0.1m; // notation is totally ad-hoc, just for the sake of the example code
var b = 0.2m;
var c = a + b;
if (c === 0.3m) {
    console.log("OK");
}

the JS output should be something like this:

    var a = new Decimal("0.1");
    var b = new Decimal("0.2");
    var c = Decimal.add(a, b);
    if (Decimal.compare(c, new Decimal("0.3") === 0) {
        console.log("OK");
    }

where Decimal is the class that implements the decimal arithmetic and conversions.
There are a number of JS decimal implementations to take inspiration from:
https://github.com/MikeMcl/decimal.js
https://github.com/MikeMcl/bignumber.js
https://www.npmjs.com/package/jsdecimal
https://github.com/iriscouch/bigdecimal.js

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions