function add(arg1, arg2) {
var r1, r2, m
try {
r1 = arg1.toString().split('.')[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split('.')[1].length
} catch (e) {
r2 = 0
}
let b = Math.pow(10, Math.max(r1, r2))
// console.log(m)
m = Math.max(r1, r2)
// return (arg1 * m + arg2 * m) / m
let num1 = +multiBigInt(arg1, m)
let num2 = +multiBigInt(arg2, m)
let sum = num1 + num2
console.log(sum / b)
}
function multiBigInt(n, p = 0) {
let str = n,
i = p
if (typeof n === 'number') {
// .123 === 0.123
str = `${n}`
}
if (p < 0) {
i = Math.abs(p)
}
const pIndex = str.indexOf('.')
if (pIndex > 1) {
if (str.slice(pIndex + 1).length <= i) {
return `${str.slice(0, pIndex)}${str.slice(pIndex + 1).padEnd(i, '0')}`
// return `${str.slice(0, pIndex)}${str.slice(pIndex) * 10 ** i}`
}
return `${str.slice(0, pIndex)}${str.slice(
pIndex + 1,
pIndex + 1 + i
)}.${str.slice(pIndex + 1 + i)}`
}
if (pIndex === 0 || pIndex === 1) {
if (str.slice(pIndex + 1).length <= i) {
return `${str * 10 ** i}`
}
return `${str.slice(pIndex + 1, pIndex + 1 + i)}.${str.slice(
pIndex + 1 + i
)}`
}
return `${str.padEnd(str.length + i, '0')}`
}
console.log(multiBigInt(123, 4)) // "1230000"
add(476.46, 20212.56)