From 9698732f036561552a2c77e757d8f7417122db1b Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Thu, 12 Oct 2023 22:11:49 -0400 Subject: [PATCH] fix: only round by 3 when float --- calculator/js/script.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/calculator/js/script.js b/calculator/js/script.js index b0b437d..069a743 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -89,7 +89,7 @@ function handleOperation() { ops.clear(); return; } - result = result.toPrecision(3); + result = roundThreeDecimals(result); updateDisplay(result); ops.firstNum = result; ops.secondNum = String(); @@ -106,6 +106,15 @@ function updateDisplay(displayValue) { display.textContent = displayValue; } +function roundThreeDecimals(number) { + if (!Number.isInteger(number) && Number.isFinite(number) + ) { + return parseFloat(number.toPrecision(3)); + } else { + return number; + } +} + function handleOperatorClick(operatorClicked) { // TODO handle ops for = or clear @@ -176,4 +185,4 @@ function percentageNumber() { buttons.forEach(btn => { btn.addEventListener('click', calculator); -}); \ No newline at end of file +});