@raulfdm/decimal-counter

How many decimals places has your number?

Usage no npm install needed!

<script type="module">
  import raulfdmDecimalCounter from 'https://cdn.skypack.dev/@raulfdm/decimal-counter';
</script>

README

@raulfdm/decimal-counter

Count decimal places can be much more annoying than you think. 🙄

Motivation

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.

Usage

First you have to install it as a dependency:

yarn add @raulfdm/decimal-counter

# Or if you prefer use npm
npm install @raulfdm/decimal-counter

Then just import and call the function:

const decimalCounter = require('decimal-counter'); // For es5
//import decimalCounter from '@raulfdm/decimal-counter'; // For es6^

decimalCounter('1.23'); //> 2
decimalCounter(1.23); //> 2
decimalCounter('1,23'); //> 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 dist, copy decimal-counter.js to your project and import it as a script.

<!-- Old fashioned JS -->

<script src="node_modules/@raulfdm/decimal-counter/dist/decimal-counter.js"></script>
<script>
  var numberOfDecimals = CountDecimal('1.24');
</script>

License

MIT