一、JAVA
1、科学技术法转数值
BigDecimal za = zaVal1.divide(val2);// 9.05E-8
String za2 = za.toPlainString();//0.0000000905
2、数值转科学技术法
BigDecimal za4 = new BigDecimal(za2);// 9.05E-8
String za5 = za.toString();//9.05E-8
二、JS
1、科学技术法转数值
function convertNUM(num_str) {
//科学计数法字符 转换 为数字字符, 突破正数21位和负数7位的Number自动转换
// 兼容 小数点左边有多位数的情况,即 a×10^b(aEb),a非标准范围(1≤|a|<10)下的情况。如 3453.54E-6 or 3453.54E6
var resValue = '',
power = '',
result = null,
dotIndex = 0,
resArr = [],
sym = '';
var numStr = num_str.toString();
if (numStr[0] == '-') { // 如果为负数,转成正数处理,先去掉‘-’号,并保存‘-’.
numStr = numStr.substr(1);
sym = '-';
}
myConsoleLog(numStr);
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);
myConsoleLog(result);
if (result != null) {
resValue = result[2];
power = result[5];
result = null;
}
if (!resValue && !power) {
return false
}
dotIndex = 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('');
myConsoleLog(sym + resValue);
return sym + resValue;
}
2、数值转科学技术法
var dbValue = 9.05E-8;
var val = math2science(dbValue);//0.0000000905
math2science:function(math){
var result = toExponential(math,2);
result = result.replace(/e/,"E");
if (result.length == 7)
{
result = result.substr(0,6) + "0" + result.substr(6);
}
return result;
},
toExponential:function(_num,d){
if(!this.IsNum(_num)) return _num;
var _tmpNum = Math.abs(Number(_num));
var _tmpS = _tmpNum.toExponential();
var _tmpSNum = this.toFixed(Number(_tmpS.substr(0,_tmpS.indexOf('e'))),d);
var _tmpSEStr =_tmpS.substr(_tmpS.indexOf('e'));
var _resultValueABS = _tmpSNum + _tmpSEStr;
return (_num > 0 )?_resultValueABS:"-" + _resultValueABS;
}
标签:numStr,转换,power,resValue,数值,num,result,科学技术,var From: https://www.cnblogs.com/xlj227/p/17336433.html