javescripeto le calco
This is a well-known problem, in Java they have developed a class called BigDecimal to solve this.
The problem is generated when you perform a simple calculation using some decimal values, example:
02 + 01 = 030000000000000004
But the same problem does not occur when the same result is reached using powers of 10, example:
3 / 10 = 03
npm install calc-js --save
// nodevar Calc = Calc;
<!-- browser -->
How to use
The use of the library is very simple:
;// 0.2 + 0.1;// 0.2 - 0.1;// 2199 / 100 * 5;
There are some EcmaScript errors that are not thrown by the language and it cannot be treated,
these erros are: zero division and infinity numbers
0 / 10 = Infinity, non numberic math
'Some text' * 10 = NaN and insecure numbers (too large number that language cannot
represent, but still allow you to use then).
You can choose four ways to deal with errors:
1. Ignore then
Ignore all identified errors
2. Listen an event
Register a function on library to listen all thrown errors. This will cover all Calc in application so, is a centralized treatment
Note: if you choose emit-event and don't associate any function to listen the errors, the application will thrown then
This is a non centralized solution. You'll use this when the calc is critical and should not generate an invalid result.
4. Show in console
This will write the error in the browser console
All library settings are related to how errors will be thrown. The following exemple show how to change these configs and show the default values set in the library:
This configure function will override the default values to each Calc execution in your application, but these default values can be overriden in each execution (configure localy will never required, but optional):
;new Calc10, localConfig.divide0.finish;
;// the old way to calc in the libraryCalc.divide10, 0, localConfig;
;// it will throw if this isn't a valid numberCalc.checkNumberInfinity, 'Validating data from query param', localConfig;
1. Create an issue
No one feature will be implemented without it having an open issue and without which the proposed has been accepted by the team responsible for the project. After the issue is approved, the applicant, a team member or anyone else can open a pull request associated with that issue (just paste the issue link in the pull request).
2. Did you find a bug?
When logging a bug, please be sure to include the following:
- The library version;
- If at all possible, an isolated way to reproduce the behavior;
- The behavior you expect to see, and the actual behavior.
You can try to update the library to the last version to see if the bug has already been fixed.
3. Do not create a duplicate issue
Search the existing issues before logging a new one.
Some search tips:
- Don't restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title.
- Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes.
4. Create a Pull Request
Follow the steps:
- Create a fork from our repository by clicking here, install node, do a
git cloneof your forked repository and run
npm installin the application folder;
- Create a branch in your forked repository, then code the feature or fix the bug;
npm run lint,
npm run testand
npm run buildin the repository;
- Create a Pull Request from your repository to this one, with the issue in the body and some information you think could be usefull to the reviewer (print or a gif of it working will be appreciated);
- The reviewer can ask some changes, don't be mad, this is the GIT Flow process;
- You get approved and your branch with the feature / fix