JavaScript 精确加法去掉小数点末尾多余的0

/**
* 精确加法
*/
addNumber(num1, num2) {
    const num1Digits = (num1.toString().split('.')[1] || '').length;
    const num2Digits = (num2.toString().split('.')[1] || '').length;
    const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
    return (num1 * baseNum + num2 * baseNum) / baseNum;
},
  • 12.00 输出 12
posted @ 2025-04-09 11:16  程序员の奇妙冒险  阅读(38)  评论(0)    收藏  举报