[英]Is there a function that returns the number of characters in BigNumber.js?
I know about decimalPlaces :我知道decimalPlaces :
const number = new BigNumber(100.5254);
number.decimalPlaces(); // 4
const number = new BigNumber(100.5254);
number.precision(); // 7
But I couldn't find any function that returns only the number of characteristics.但我找不到任何只返回特征数量的 function 。 That is, in this example, 3 ("1", "0" and "0").
即,在本例中为 3(“1”、“0”和“0”)。
Is there something for this?有什么东西吗?
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:至少截至 2019 年 11 月,似乎没有一个,但您可以从总精度中减去小数位以获得点之前的数字:
const number = new BigNumber(100.5254);
const digitsBeforeDot = number.precision(true) - number.decimalPlaces();
// prints 3
Note that:注意:
If
d
(the first parameter of theprecision
function) is true then any trailing zeros of the integer part of a number are counted as significant digits, otherwise they are not.如果
d
(precision
函数的第一个参数)为真,则数字的 integer 部分的任何尾随零都计为有效数字,否则不计为有效数字。
See the docs:请参阅文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.