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

  1. DanielRosenwasser commented on Apr 22, 2015

    @DanielRosenwasser
    Member

    See TypeScript's non-goals:

    Provide additional runtime functionality or libraries. Instead, use TypeScript to describe existing libraries.

    As you've said:

    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

    As our non-goal suggests, those libraries should be sufficiently usable from TypeScript itself. DefinitelyTyped currently currently has typings for decimal.js

  2. fletchsod-developer commented on Apr 23, 2015

    @fletchsod-developer

    Does Typescript have like bool.parse(), decimal.parse(), etc.? Does Typescript have datatype conversion features? Just wondering cuz we don't want to have baggage issues we're struggling with, from adding too many 3rd party libraries.

  3. RyanCavanaugh commented on Apr 23, 2015

    @RyanCavanaugh
    Member

    TypeScript doesn't provide any built-in run-time libraries.

  4. mcwienczek commented on Feb 20, 2017

    @mcwienczek

    As our non-goal suggests, those libraries should be sufficiently usable from TypeScript itself. DefinitelyTyped currently currently has typings for decimal.js

    Looks like it's significantly outdated. The link doens't even work anymore. Any alternative to this problem so far?

  5. locked and limited conversation to collaborators on Jun 18, 2018
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