reimplementing percentage handling, it cannot be handled via the library

This commit is contained in:
tibbi 2020-11-06 21:40:40 +01:00
parent 871683d532
commit 0b825d119e
1 changed files with 12 additions and 12 deletions

View File

@ -103,7 +103,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!) private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!)
fun handleResult() { private fun handleResult() {
if (moreOperationsInRaw) { if (moreOperationsInRaw) {
val index = displayedNumber!!.indexOfAny(operations) val index = displayedNumber!!.indexOfAny(operations)
displayedNumber = displayedNumber!!.substring(index + 1) displayedNumber = displayedNumber!!.substring(index + 1)
@ -157,6 +157,15 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
isFirstOperation = false isFirstOperation = false
} }
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. % is used only for modulo there
private fun handlePercent() {
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
val result = operation.getResult()
callback!!.setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%", context)
inputDisplayedFormula = result.format()
updateResult(result)
}
fun handleOperation(operation: String) { fun handleOperation(operation: String) {
if (inputDisplayedFormula.isEmpty()) { if (inputDisplayedFormula.isEmpty()) {
inputDisplayedFormula = "0" inputDisplayedFormula = "0"
@ -189,12 +198,12 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
} }
/*if (lastKey == DIGIT && lastOperation != "" && operation == PERCENT) { if (lastKey == DIGIT && lastOperation != "" && operation == PERCENT) {
val tempOperation = lastOperation val tempOperation = lastOperation
handlePercent() handlePercent()
lastKey = tempOperation lastKey = tempOperation
lastOperation = tempOperation lastOperation = tempOperation
} else */if (lastKey == DIGIT) { } else if (lastKey == DIGIT) {
handleResult() handleResult()
if (inputDisplayedFormula.last() != '+' && if (inputDisplayedFormula.last() != '+' &&
inputDisplayedFormula.last() != '-' && inputDisplayedFormula.last() != '-' &&
@ -213,15 +222,6 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
setValue(inputDisplayedFormula) setValue(inputDisplayedFormula)
} }
private fun handlePercent() {
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation)
val result = operation.getResult()
setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
secondValue = result
updateResult(result)
inputDisplayedFormula = displayedNumber ?: ""
}
fun handleClear() { fun handleClear() {
if (displayedNumber.equals(NAN)) { if (displayedNumber.equals(NAN)) {
handleReset() handleReset()