Giter Site home page Giter Site logo

srknzl / bigdecimal.js Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 5.0 34.08 MB

BigInt based BigDecimal implementation

Home Page: https://srknzl.github.io/bigdecimal.js

License: Apache License 2.0

JavaScript 72.62% TypeScript 26.01% Java 1.37%
bigdecimal arbitrary-precision math floating-point

bigdecimal.js's Introduction

bigdecimal.js's People

Contributors

dependabot[bot] avatar rishikumarray avatar srknzl avatar timvandam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bigdecimal.js's Issues

"Can't find variable: BigInt" error thrown on Safari 13

Issue:
"Can't find variable: BigInt" error is thrown on Safari 13 while app load

  1. BigDecimal.js version: 1.3.0
  2. Node.js version: 18.17.1
  3. Operating system: macOS Catalina

Can we polyfill BigInt for browsers that do not support BigInt? Can JSBI be used for this case?

Extends API to support compareTo aliases (===, >, >=, etc...)

Is your feature request related to a problem? Please describe.
Relying on compareTo is quite verbose and not very comfortable to read or use in my humble opinion.
I think this library will greatly benefit by adding aliases for each comparaison operators.

Describe the solution you'd like
Ideally, I would like to extends the API to support gt, gte, lt, lte as aliases of compareTo (for reference: bignumber, big)
I would also like to make equals less strict by making it an alias for === 0 and rename the current method to strictEquals when scale matter

   if (x.equals(y)) // ===
   if (!x.equals(y)) // !==
   if (x.strictEquals(y)) // equals in value & scale
   if(x.gt(y))  // >
   if(x.gte(y)) // >=
   if(x.lt(y))   // <
   if(x.lte(y))  // <=

If the intention is to keep the API closely related to the JAVA implementation, it is also perfectly fine to find a different alternative for the === alias. (ex: sameAs, similar, equivalent, eq, etc...)

I'm also considering adding notEquals to the list

Describe alternatives you've considered
Create a new Compare utils

class Comparator {
  constructor(private value: BigDecimal) {}

  equals(value: BigDecimal) {
    return this.value.compareTo(value) === 0;
  }

  notEquals(value: BigDecimal) {
    return this.value.compareTo(value) !== 0;
  }

  gt(value: BigDecimal) {
    return this.value.compareTo(value) > 0;
  }

  gte(value: BigDecimal) {
    return this.value.compareTo(value) >= 0;
  }

  lt(value: BigDecimal) {
    return this.value.compareTo(value) < 0;
  }

  lte(value: BigDecimal) {
    return this.value.compareTo(value) <= 0;
  }
}

function Compare(value: BigDecimal): Comparator {
  return new Comparator(value);
}

// if(Compare(x).gte(y))

I'm currently using this util in my own projects to avoid extending the bigdecimal.js class

Make `add`, `subtract`, `multiply`, `divide` & `compareTo` handle `any`

Hello!

I just noticed that the following functions add, subtract, multiply, divideWithMathContext , divideToIntegralValue, divideAndRemainder & compareTo use convertToBigDecimal to convert the incoming value as a safety measure.
This code doesn't seem to be present in the Java source code though.
I guess it was done to prevent errors when developers aren't using typescript or to mitigate mistakes if they use it due to the nature of JS ?

In order to match the (un)expected type, I guess it would make more sense to be more flexible and allow any or BigDecimal | string | number directly in those methods too ? this would also allow the following notation which happen to be less convoluted:

- bigValue.add(Big(1)).compareTo(Big(0))
+ bigValue.add(1).compareTo(0) 

Create a bigdecimal-light.js

Is your feature request related to a problem? Please describe.
This package looks great. Thanks for creating it. I wish the bundle size was quite a bit smaller though. Something on the order of big.js

Describe the solution you'd like
One idea would be to create a new package, maybe named bigdecimal-light.js, that has a comparable set of methods to big.js.

Describe alternatives you've considered
I don't think forking and removing the methods on my end would be a great solution but I guess it is possible.

Any interest in this idea?

No identifiers allowed directly after numeric literal

Describe the bug
I'm using your library to handle big numbers in my library: https://github.com/macdonaldr93/bloombug-money.

I'm building a React Native app with it and when I run the app I get no identifiers allowed directly after numeric literal with no stacktrace. I've debugged the issue to this library. I'm able to start a new project in expo, include this library, create a BigDecimal and it breaks.

Expected behavior
Work on Android.

Screenshots

Environment (please complete the following information):

  • ReactNative and Expo
  • Operating system Android
  • BigDecimal.js version 1.1.0
  • Node.js v16.13.0

To Reproduce

  1. Start a new project in expo
  2. Install this library
  3. Create a BigDecimal and it breaks.

Additional context
For some reason, big.js doesn't have this issue.

ES Modules

Hi,

Thank you so much for this package. Its the one and permanent solution for the math blunder in JS. I was wondering if we could create ES Modules of the bigdecimal.js?

I tried creating wrapper around your files which would be a .mjs file. I could not make them work. Let me know if you would be interested in making ES module compatible files.

Regards
Kavya

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.