Search code examples
javascriptdigitsbignumbignumber.js

Is there a function that returns the number of characters in BigNumber.js?


I know about decimalPlaces:

const number = new BigNumber(100.5254);
number.decimalPlaces(); // 4

And precision:

const number = new BigNumber(100.5254);
number.precision(); // 7

But I couldn't find any function that returns only the number of characteristics. That is, in this example, 3 ("1", "0" and "0").

Is there something for this?


Solution

  • It seems there isn't one, as of Nov 2019 at least, but you can subtract the decimal places from the total precision to get the digits before the dot:

    const number = new BigNumber(100.5254);
    const digitsBeforeDot = number.precision(true) - number.decimalPlaces();
    // prints 3
    

    Note that:

    If d (the first parameter of the precision function) is true then any trailing zeros of the integer part of a number are counted as significant digits, otherwise they are not.

    See the docs: