【问题标题】:How to remove scientific exponent of a number?如何去除数字的科学指数?
【发布时间】:2021-05-25 02:08:48
【问题描述】:

Typescript 提供TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))

let [ , num ] = hiResNum.toString().match(/^([+-]?[0-9]+[.]?[0-9]*)(e[+-]?[0-9]+)?$/);
num = parseFloat(num).toFixed(digits);

让 num: 任何类型 'RegExpMatchArray | null' 必须有一个 'Symbol.iterator' 方法返回一个 iterator.ts(2488)

我应该如何分隔数字的科学记数法部分?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    问题是String.match 函数可以返回Array | null 并且null 是不可迭代的。所以,你需要知道r 是否为空;那么你可以使用r[1]

    const r = hiResNum.toString().match(/^([+-]?[0-9]+[.]?[0-9]*)(e[+-]?[0-9]+)?$/);
    if (r && r.length > 1) {
        num = parseFloat(r[1]).toFixed(digits);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 2017-08-06
      相关资源
      最近更新 更多