From f011544934c592289d552feaaba7f3f8f1a2cb7d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 14 Nov 2021 09:35:43 +0100 Subject: [PATCH] fix percentage handling --- app/build.gradle | 2 +- .../calculator/helpers/CalculatorImpl.kt | 62 +++++++++---------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 02fe9c2a..d40a6d30 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,7 +55,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:cac7ba71ac' + implementation 'com.github.SimpleMobileTools:Simple-Commons:2f86d76a56' implementation 'me.grantland:autofittextview:0.2.1' implementation 'net.objecthunter:exp4j:0.4.8' } diff --git a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt index d611e1c0..200cceff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -93,27 +93,23 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { } if (lastKey == DIGIT || lastKey == DECIMAL) { - if (lastOperation != "" && operation == PERCENT) { - handlePercent() - } else { - // split to multiple lines just to see when does the crash happen - secondValue = when (operation) { - PLUS -> getSecondValue() - MINUS -> getSecondValue() - MULTIPLY -> getSecondValue() - DIVIDE -> getSecondValue() - ROOT -> getSecondValue() - POWER -> getSecondValue() - PERCENT -> getSecondValue() - else -> getSecondValue() - } + // split to multiple lines just to see when does the crash happen + secondValue = when (operation) { + PLUS -> getSecondValue() + MINUS -> getSecondValue() + MULTIPLY -> getSecondValue() + DIVIDE -> getSecondValue() + ROOT -> getSecondValue() + POWER -> getSecondValue() + PERCENT -> getSecondValue() + else -> getSecondValue() + } - calculateResult() + calculateResult() - if (!operations.contains(inputDisplayedFormula.last().toString())) { - if (!inputDisplayedFormula.contains("÷")) { - inputDisplayedFormula += getSign(operation) - } + if (!operations.contains(inputDisplayedFormula.last().toString())) { + if (!inputDisplayedFormula.contains("÷")) { + inputDisplayedFormula += getSign(operation) } } } @@ -142,19 +138,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { return false } - // handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there - private fun handlePercent() { - var result = calculatePercentage(baseValue, getSecondValue(), lastOperation) - if (result.isInfinite() || result.isNaN()) { - result = 0.0 - } - - showNewFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%") - inputDisplayedFormula = result.format() - showNewResult(result.format()) - baseValue = result - } - fun handleEquals() { if (lastKey == EQUALS) { calculateResult() @@ -207,13 +190,26 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) { if (lastOperation != "") { val sign = getSign(lastOperation) val expression = "${baseValue.format()}$sign${secondValue.format()}".replace("√", "sqrt").replace("×", "*").replace("÷", "/") + try { if (sign == "÷" && secondValue == 0.0) { context.toast(R.string.formula_divide_by_zero_error) return } - val result = ExpressionBuilder(expression.replace(",", "")).build().evaluate() + if (sign == "%") { + val second = secondValue / 100f + "${baseValue.format()}*${second.format()}".replace("√", "sqrt").replace("×", "*").replace("÷", "/") + } + + // handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there + val result = if (sign == "%") { + val second = secondValue / 100f + ExpressionBuilder("${baseValue.format()}*${second.format()}").build().evaluate() + } else { + ExpressionBuilder(expression.replace(",", "")).build().evaluate() + } + if (result.isInfinite() || result.isNaN()) { context.toast(R.string.unknown_error_occurred) return