typescript - How to remove scientific exponent of a number?
问题描述
Typescript is giving 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);
let num: any Type 'RegExpMatchArray | null' must have a 'Symbol.iterator' method that returns an iterator.ts(2488)
How should I separate the scientific notation part of a number?
解决方案
问题是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);
}
推荐阅读
- python - 在 Twisted 中执行具有时间增量的函数的最佳方法?
- c# - 实体框架级联删除(继承)
- angular - 将角度从 6.1 更新到 8.2
- angular - 如何将带有标题 Ag Grid Angular 的表格分成两部分
- javascript - 检查数组是否在 Angular 中有值
- python - 使用 Python 和 Selenium 包创建一个脚本来关闭 Chrome 暗模式。如何在 chrome:// 中选择元素?
- c - 警告:格式“%lf”需要“double”类型的参数
- node.js - 在角度通用应用程序中设置动态数据
- java - 将 Java 参数值分配给变量
- debugging - 无法在 Autohotkey 中发送 win+s