Git Product home page Git Product logo

Comments (4)

arenodi avatar arenodi commented on August 21, 2024 1

Hey I would like to tackle this issue. Do you have more information?

from calculator.

N3v1 avatar N3v1 commented on August 21, 2024

Hey I would like to tackle this issue. Do you have more information?

Sure @arenodi,
before I merged the PR (#18) the percent functionality worked fine but after it, it doesn't work properly.
Example: 100 - 50% = 50
Bildschirmfoto 2023-08-13 um 12 04 29

The calculator says 99.5 but it should be 50.
Before I merged #18 this example worked fine and it printed 50

from calculator.

arenodi avatar arenodi commented on August 21, 2024

hey @N3v1 , there are a few things to take into consideration:

Lines 73 to 77:

  • The if true body will never run, because the typeof of the arg number will always be a string.
  • In else body, the percentageToDecimal() function is called and then...

Lines 48 to 50:

  • The percentageToDecimal() function receives the string and the parses it with new Decimal().
  • new Decimal() returns a decimal and then the decimal is divided by 100.
  • percentageToDecimal() then returns the quotient of that operation.

That is, the '50' as a string will trigger the percentageToDecimal() function that will invoke the new Decimal() and be divided by 100, and then returned as 0.5, hence 100 - 0.5 = 99.5.

Fix:

To fix this problem I made a few changes on the code:

  1. Add 'math.js' library to parse and evaluate expressions.
  2. Remove library 'decimal.js'.
  3. Remove percentageToDecimal() function.
  4. Partially refactoring.

Note: With 'math.js' library the expression is compiled from the way it is typed and then evaluated, eliminating the need to custom parsing it. Also the lib is powerful enough to support future features as advanced arithmetics operations.

I will create a pull request on it.

from calculator.

N3v1 avatar N3v1 commented on August 21, 2024

Thank you @arenodi for your work

from calculator.

Related Issues (20)

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.