科学计数法怎么转字符串

如 1.123e-10,转成 0.0000000001123。

阅读 2.4k
2 个回答
function numToStr(num) {
  const numStr = num.toString();
  // 没有指数就直接返回
  if (!/e/.test(numStr)) return numStr;
  const nums = numStr.split(/\.|e/);
  // 判断是否有小数点
  const hasDecimal = nums.length === 3;
  // 小数位数
  const decimalCount = hasDecimal ? nums[1].length : 0;
  // 指数值
  const powerCount = hasDecimal ? nums[2] : nums[1];
  let fixCount = powerCount - decimalCount;
  fixCount = fixCount > 0 ? 0 : fixCount;
  return Number(num).toFixed(-fixCount)
}
numToStr(1.123e-10); // 0.0000000001123
numToStr(1.123e10); // 11230000000
numToStr(1.123e-3); // 0.001123
numToStr(2e-3); // 0.002
numToStr(112.3e-3); // 0.1123
Number("1.123e-10").toFixed(10 + x)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题