自己写的科学计数法小程序
程序实现的目的,开始和差值的计算保证位数不变。用0补位,保持位数。
在网上找了好多但是都是限制使用js的number的,导致无法计算超过20位的运算。所以自己写一个
实现真正的js不限制位数,但是其中数字的大小校验就要自己完成,目前程序中是alert出错信息。
<HTML>
<HEAD>
<title>数据加载中...Please wait...</title>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<SCRIPT type=text/javascript>
function _mathAdd(numa,numb){
var arr = new Array();
var brr = new Array();
var result = new Array();
var arri = 0;
var brri = 0;
var jinwei = 0;
for(var i=numa.length-1;i>=0;i--){
arr[arri++] = numa.substring(i,i+1);
}
for(var i=numb.length-1;i>=0;i--){
brr[brri++] = numb.substring(i,i+1);
}
var j=0;
for(j=0;j<(numa.length>numb.length?numa.length:numb.length);j++){
//本位
var temp = 0;
temp = parseInt(temp) + parseInt(jinwei);
if(j<numa.length){
temp = parseInt(temp) + parseInt(arr[j]);
}
if(j<numb.length){
temp = parseInt(temp) + parseInt(brr[j]);
}
temp = "" + temp;
if(temp.length>1){
jinwei = 1;
result[j] = temp.substring(1,2);
} else {
jinwei = 0;
result[j] = temp;
}
}
if(jinwei != 0){
result[j++]=1;
alert('sorry it\'s out of limit');
}
var cvaule = "";
for(var i=0;i<result.length;i++){
cvaule = result[i] + cvaule;
}
return cvaule;
}
function _mathDes(numd,nume){
var drr = new Array();
var err = new Array();
var result = new Array();
var drri = 0;
var erri = 0;
var jiewei = 0;
for(var i=numd.length-1;i>=0;i--){
drr[drri++] = numd.substring(i,i+1);
}
for(var i=nume.length-1;i>=0;i--){
err[erri++] = nume.substring(i,i+1);
}
for(j=0;j<numd.length;j++){
//本位
var temp = 0;
if(j<nume.length){
temp = parseInt(drr[j]) - parseInt(err[j]) - parseInt(jiewei);
} else {
temp = parseInt(drr[j]) - parseInt(jiewei);
}
if(temp >= 0){
jiewei = 0;
} else {
if(j<nume.length){
temp = parseInt("1" + drr[j]) - parseInt(err[j]) - parseInt(jiewei);
} else {
temp = parseInt("1" + drr[j]) - parseInt(jiewei);
}
jiewei = 1;
}
result[j] = temp;
}
if(jiewei >0){
alert('sorry it\'s out of limit');
return;
}
var j=0;
var cvaule = "";
for(var i=0;i<result.length;i++){
cvaule = result[i] + cvaule;
}
return cvaule;
}
function numberAdd(numa,numb){
var cvaule = _mathAdd(numa,numb);
return _mathDes(cvaule,"1");
}
function numberDes(numa,numb){
var cvaule = _mathDes(numa,numb);
return _mathAdd(cvaule,"1");
}
function deZero(numa){
numa = "" + numa;
var drr = new Array();
var result = new Array();
var flag = true;
var drri = 0;
var resulti = 0;
for(var i=0;i<numa.length;i++){
drr[drri++] = numa.substring(i,i+1);
}
for(var i=0;i<numa.length;i++){
if(flag){
if(("" + drr[i]) != "0"){
flag = false;
} else {
continue;
}
}
result[resulti] = drr[i];
resulti++;
}
var cvaule = "0";
for(var i=0;i<resulti;i++){
cvaule = result[i];
}
return cvaule;
}
function show(){
var avaule = document.all.a.value;
var bvaule = document.all.b.value;
if(avaule>bvaule){
alert("a>b");
} else {
alert("a!>b");
}
return;
document.all.c.value = deZero(numberAdd(avaule,bvaule));
}
function showA(){
var dvaule = document.all.d.value;
var evaule = document.all.e.value;
document.all.f.value = numberDes(dvaule,evaule);
}
</SCRIPT>
</HEAD>
<BODY >
<input type="text" id="a" />+
<input type="text" id="b" />=
<input type="text" id="c" />
<input type="button" value="rs" onclick="show();"/>
<br/>
<input type="text" id="d" />-
<input type="text" id="e" />=
<input type="text" id="f" />
<input type="button" value="des" onclick="showA();"/>
</BODY>
</HTML>
补充:web前端 , JavaScript ,