本文转载至:原文链接
function toolNumber(num_str) { num_str = num_str.toString(); if (num_str.indexOf("+") != -1) { num_str = num_str.replace("+", ""); } if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) { var resValue = "", power = "", result = null, dotIndex = 0, resArr = [], sym = ""; var numStr = num_str.toString(); if (numStr[0] == "-") { //如果为负数,转成正数处理,先去掉‘-’号,并保存‘-’. numStr = numStr.substr(1); sym = "-"; } if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) { var regExp = new RegExp( "^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$", "ig" ); result = regExp.exec(numStr); if (result != null) { resValue = result[2]; power = result[5]; result = null; } if (!resValue && !power) { return false; } dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf("."); resValue = resValue.replace(".", ""); resArr = resValue.split(""); if (Number(power) >= 0) { var subres = resValue.substr(dotIndex); power = Number(power); //幂数大于小数点后面的数字位数时,后面加0 for (var i = 0; i <= power - subres.length; i++) { resArr.push("0"); } if (power - subres.length < 0) { resArr.splice(dotIndex + power, 0, "."); } } else { power = power.replace("-", ""); power = Number(power); //幂数大于等于 小数点的index位置, 前面加0 for (var i = 0; i < power - dotIndex; i++) { resArr.unshift("0"); } var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex); resArr.splice(n, 0, "."); } } resValue = resArr.join(""); return sym + resValue; } else { return num_str; } }
来源:博客园
作者:想默默地成为大神
链接:https://www.cnblogs.com/hnzkljq/p/11525883.html