count-digits

Count the digits in a number (in any base)

Usage no npm install needed!

<script type="module">
  import countDigits from 'https://cdn.skypack.dev/count-digits';
</script>

README

count-digits

master branch build status

Counts the digits of an integer in any base.

Usage

countDigits(value[, base=10])

Example

var countDigits = require('count-digits');

countDigits(100);  // => 3
countDigits(1000); // => 4
countDigits(1e4);  // => 5
countDigits(0xff, 16); // => 2
countDigits(0xff, 2);  // => 8

Installing

$ npm install --save count-digits

Notes

Zero

Zero is a special case when counting digits. Be aware that countDigits(0) will return 0 where you might be expecting 1.

Floats

Since only whole values are counted, countDigits(Math.PI) will return 1.

License

MIT.