Count decimal places can be much more annoying than you think. 🙄
After face this problem and didn't see a clear code to do it, I've decided to create my own implementation, considering some edge cases and 100% tested.
First you have to install it as a dependency:
yarn add @raulfdm/decimal-counter# Or if you prefer use npmnpm install @raulfdm/decimal-counter
Then just import and call the function:
const decimalCounter = ; // For es5//import decimalCounter from '@raulfdm/decimal-counter'; // For es6^; //> 2; //> 2; //> 2
If you're old fashioned and write applications without a package manager, unfortunately this project doesn't have a CDN, so please go to the folder
decimal-counter.js to your project and import it as a script.
<!-- Old fashioned JS -->