0

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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
  • 1
    There are no leading or trailing zeros, so the isn't the number of significant digits *actually* 7? If the calculation being performed isn't meant to be accurate to 7 significant digits, then that's something that should be addressed manually elsewhere (it's not something that a plain method could determine, unless you include a statistics package of some kind and give it context...) – CertainPerformance Nov 03 '19 at 22:34
  • 2
    how about precision() - decimalPlaces() ? – prhmma Nov 03 '19 at 22:37
  • The `.sd()` method returns the number of significant digits, which is 7 in your example. (Hint: it's an alias for `precision`). – Bergi Nov 03 '19 at 22:41
  • Sorry all, I updated the question. I am looking for the number of "characteristics" - the digits before the dot. – Paul Razvan Berg Nov 03 '19 at 23:10
  • Do the math : 3 = 7 - 4 you have all you needed – Thanh Trung Nov 03 '19 at 23:19
  • @ThanhTrung Yeah I did that, but I use it in my places so I would've avoided creating my own var for this, if possible. Anyway, thanks. – Paul Razvan Berg Nov 04 '19 at 10:00

1 Answers1

2

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:

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114