科学技术法与数值之前的转换
一、JAVA
1、科学技术法转数值
方法1
BigDecimal za = zaVal1.divide(val2);// 9.05E-8
String za2 = za.toPlainString();//0.0000000905
方法2
String val2 = "9.05E-8";
BigDecimal val0 = new BigDecimal(a);//0.0000000905
String val2 = BigDecimal.valueOf(new Double(String.valueOf(val0))).toString();
其他问题:精度缺失
如果a和b为double类型,很可能出现精度丢失的情况:0.45699999999999999999
将a和b转为string类型,就不会出现精度丢失。
BigDecimal aa = new BigDecimal(Double.toString(a));
BigDecimal bb = new BigDecimal(Double.toString(b));
BigDecimal cc = aa.divide(bb);
2、数值转科学技术法
String za2 = "0.0000000905";
BigDecimal za4 = new BigDecimal(za2);// 9.05E-8
String za5 = za.toString();//9.05E-8
上面的要是数据短一点E-6不会自动转过来,上面的不要用,用需要下面
String za2 = "0.000009056666666";
BigDecimal za4 = new BigDecimal(za2);// 0.0000090566666
String val = String.format("%E", val0);//9.05666666E-6
//还需要保留keep位小数
String aa = val.substring(0,val.indexOf(".")+4);
BigDecimal bg = new BigDecimal(aa);
double cc = bg.setScale(keep, BigDecimal.ROUND_HALF_UP).doubleValue();
String bb = val.substring(val.indexOf("E"), val.length());
val = cc + bb;
二、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;
}