ts
/*
* @description 十进制数字相加
* @param { number } num1 -数字1
* @param { number } num2 -数字2
* @returns { number }
* @example
* add(0.1,0.2) => 0.3
*/
function add(num1: number, num2: number): number {
const len1 = (num1.toString().split(".") || "").length;
const len2 = (num2.toString().split(".") || "").length;
const maxLen = Math.max(len1, len2);
const multiple = Math.pow(10, maxLen);
return (num1 * multiple + num2 * multiple) / multiple;
}
js
/*
* @description 十进制数字相加
* @param { number } num1 -数字1
* @param { number } num2 -数字2
* @returns { number }
* @example
* add(0.1,0.2) => 0.3
*/
function add(num1, num2) {
var len1 = (num1.toString().split(".") || "").length;
var len2 = (num2.toString().split(".") || "").length;
var maxLen = Math.max(len1, len2);
var multiple = Math.pow(10, maxLen);
return (num1 * multiple + num2 * multiple) / multiple;
}
评论区