From 5ecff10644a7672071394640cab7bd0279c3ba71 Mon Sep 17 00:00:00 2001 From: Danil Ochagov Date: Mon, 18 May 2020 17:45:52 +0300 Subject: [PATCH] Add division by zero message --- .../calculator/helpers/CalculatorImpl.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) 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 3d527d05..6558caee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calculator/helpers/CalculatorImpl.kt @@ -56,13 +56,21 @@ class CalculatorImpl(calculator: Calculator, val context: Context) { val second = secondValue.format() val sign = getSign(lastOperation) - if (sign == "√") { - setFormula(sign + first) - } else if (sign == "!") { - setFormula(first + sign) - } else if (!sign.isEmpty()) { - var formula = first + sign + second - setFormula(formula) + when { + baseValue == 0.0 && secondValue == 0.0 && sign == "/" -> { + setFormula("Error: division by zero") + } + + sign == "√" -> setFormula(sign + first) + + sign == "!" -> setFormula(first + sign) + + else -> { + if (sign.isNotEmpty()) { + val formula = first + sign + second + setFormula(formula) + } + } } }