Handle floating point issues on multiplication and division

This commit is contained in:
lolabird 2023-12-27 02:16:30 -07:00
commit b6a5f65d67

View file

@ -56,11 +56,37 @@ function addDecimals(a, b) {
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const maxdec = Math.max(adec, bdec);
const maxDec = Math.max(adec, bdec);
let divdec = "1";
divdec = divdec.padEnd(maxdec, "0");
divdec = divdec.padEnd(maxDec, "0");
return ((+a * divdec + +b * divdec) / divdec).toFixed(maxDec);
}
function multiplyDecimals(a, b) {
a = String(a);
b = String(b);
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const totalDec = adec + bdec;
return (a * b).toFixed(totalDec)
}
function divideDecimals(a, b) {
a = String(a);
b = String(b);
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const maxDec = Math.max(adec, bdec);
const divisor = Math.pow(10, maxDec);
return(a * divisor) / (b * divisor);
return ((+a * divdec + +b * divdec) / divdec).toFixed(maxdec);
}
@ -70,8 +96,8 @@ function Calculator(str) {
this.methods = {
"-": (a, b) => addDecimals(a, -b),
"+": (a, b) => addDecimals(a, b),
"/": (a, b) => (a / b),
"*": (a, b) => (a * b)
"/": (a, b) => divideDecimals(a, b),
"*": (a, b) => multiplyDecimals(a, b)
};
this.calculate = function(str) {