/**
* @param a
* @param b
*
* 整体思路:
* 如果两个数有小数, 先把小数转换为整数, 再进行运算, 运算后再转为小数. 整数运算就不存在精度丢失了.
*/
function numberSubtract(a, b){
if (a == null && b == null) return;
if (a == null) return b;
if (b == null) return a;
if(a != null && b != null){
let str_a = "", str_b = "";
let scale_a = 0, scale_b = 0;
let no_e_a = true, no_e_b = true;
let m = 0;
str_a = a.toString();//toString 是函数,而不是属性
str_b = b.toString();
if(str_a.indexOf(".") != -1){//js 没有 contains 函数, 可以使用 indexOf 来代替
try {
scale_a = str_a.split(".")[1].length;
} catch (e) {
no_e_a = false;
}
}else no_e_a = false;
if(str_b.indexOf(".") != -1){
try {
scale_b = str_b.split(".")[1].length;
} catch (e) {
no_e_b = false;
}
}else no_e_b = false;
if (no_e_a || no_e_b) {
if (scale_a > scale_b) {
m = scale_a;
for (var int = 0; int < scale_a - scale_b; int++) {
str_b += "0";
}
} else {
m = scale_b;
for (var int = 0; int < scale_b - scale_a; int++) {
str_a += "0";
}
}
}
//Math pow 取 10 的 m 次幂。
return (Number(str_a.replace(".", "")) - Number(str_b.replace(".", ""))) / Math.pow(10, m);
}
}