一、程式碼.gs
/*
* plus (加)
* minus (減)
* times (乘)
* div (除)
*/
function myFunction() {
var a = 0.1;
var b = 0.2;
console.log("a=",a);
console.log("b=",b);
console.log("a+b=",a+b);
console.log("加-plus : a+b=",plus(a,b));
console.log("減-minus : a-b=",minus(a,b));
console.log("乘-times : a*b=",times(a,b));
console.log("除-div : a/b=",div(a,b));
}
二、numFix.gs
/*
* plus (加)
* minus (減)
* times (乘)
* div (除)
*/
//plus (加)
function plus(n,m){
n=typeof n =="string"?n:numToString(n);
m=typeof m =="string"?m:numToString(m);
var F= n.indexOf(".")!=-1?handleNum(n):[n,0,0],
S= m.indexOf(".")!=-1?handleNum(m):[m,0,0],
l1=F[2],
l2=S[2],
L=l1>l2?l1:l2,
T=Math.pow(10,L);
return (F[0]*T+F[1]*T/Math.pow(10,l1)+S[0]*T+S[1]*T/Math.pow(10,l2))/T
}
//minus (減)
function minus(n,m){
n=typeof n =="string"?n:numToString(n);
m=typeof m =="string"?m:numToString(m);
var F= n.indexOf(".")!=-1?handleNum(n):[n,0,0],
S= m.indexOf(".")!=-1?handleNum(m):[m,0,0],
l1=F[2],
l2=S[2],
L=l1>l2?l1:l2,
T=Math.pow(10,L);
return (F[0]*T+F[1]*T/Math.pow(10,l1)-S[0]*T-S[1]*T/Math.pow(10,l2))/T
}
//times (乘)
function times(n,m){
n=typeof n =="string"?n:numToString(n);
m=typeof m =="string"?m:numToString(m);
var F= n.indexOf(".")!=-1?handleNum(n):[n,0,0],
S= m.indexOf(".")!=-1?handleNum(m):[m,0,0],
l1=F[2],
l2=S[2],
L=l1>l2?l1:l2,
T=Math.pow(10,L);
return ((F[0]*T+F[1]*T/Math.pow(10,l1))*(S[0]*T+S[1]*T/Math.pow(10,l2)))/T/T
}
//div (除)
function div(n,m){
n=typeof n =="string"?n:numToString(n);
m=typeof m =="string"?m:numToString(m);
var F= n.indexOf(".")!=-1?handleNum(n):[n,0,0],
S= m.indexOf(".")!=-1?handleNum(m):[m,0,0],
l1=F[2],
l2=S[2],
L=l1>l2?l1:l2,
T=Math.pow(10,L);
return ((F[0]*T+F[1]*T/Math.pow(10,l1))/(S[0]*T+S[1]*T/Math.pow(10,l2)))
}
function numToString(tempArray){
if(Object.prototype.toString.call(tempArray) == "[object Array]"){
var temp=tempArray.slice();
for(var i,l=temp.length;i<l;i++){
temp[i]=typeof temp[i] == "number"?temp[i].toString():temp[i];
}
return temp;
}
if(typeof tempArray=="number"){
return tempArray.toString();
}
return []
}
function handleNum(n){
n=typeof n !=="string"?n+"":n;
var temp= n.split(".");
temp.push(temp[1].length);
return temp
}