除法函数-解决精度丢失问题

常用方法   2024-07-20 11:23   114   0  
/**
 * 除法函数(精度丢失问题)
 * @param { number } num1
 * @param { number } num2
 */
export function division(num1,num2){
    let t1,t2,r1,r2;
    try {
        t1 = num1.toString().split('.')[1].length;
    } catch(e){
        t1 = 0;
    }
    try {
        t2=num2.toString().split(".")[1].length;
    } catch(e){
        t2=0;
    }
    r1 = Number(num1.toString().replace(".",""));
    r2 = Number(num2.toString().replace(".",""));
    return (r1/r2)*Math.pow(10,t2-t1);
}