【问题标题】:Is there a function that returns the number of characters in BigNumber.js?有没有返回 BigNumber.js 中字符数的函数?
【发布时间】:2019-11-03 22:32:39
【问题描述】:

我知道decimalPlaces

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

还有precision

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

但我找不到任何只返回特征数量的函数。也就是说,在本例中为 3(“1”、“0”和“0”)。

有什么办法吗?

【问题讨论】:

  • 没有前导零或尾随零,所以有效位数不是实际上 7吗?如果正在执行的计算并不意味着精确到 7 位有效数字,那么这应该在其他地方手动解决(这不是普通方法可以确定的,除非您包含某种统计数据包并为其提供上下文...)
  • precision() - decimalPlaces() 怎么样?
  • .sd() 方法返回有效位数,在您的示例中为 7。 (提示:它是precision 的别名)。
  • 抱歉,我更新了问题。我正在寻找“特征”的数量 - 点之前的数字。
  • 算一下:3 = 7 - 4 你有你需要的一切

标签: javascript digits bignum bignumber.js


【解决方案1】:

似乎没有,至少截至 2019 年 11 月,但您可以从总精度中减去小数位以获得点之前的数字:

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

请注意:

如果dprecision 函数的第一个参数)为真,则数字整数部分的任何尾随零都计为有效数字,否则不计为有效数字。

查看文档:

【讨论】:

    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多