fixing another usecase of percentage
This commit is contained in:
parent
0a6a49c17c
commit
14bb42c832
|
@ -93,6 +93,9 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastKey == DIGIT || lastKey == DECIMAL) {
|
if (lastKey == DIGIT || lastKey == DECIMAL) {
|
||||||
|
if (lastOperation != "" && operation == PERCENT) {
|
||||||
|
handlePercent()
|
||||||
|
} else {
|
||||||
// split to multiple lines just to see when does the crash happen
|
// split to multiple lines just to see when does the crash happen
|
||||||
secondValue = when (operation) {
|
secondValue = when (operation) {
|
||||||
PLUS -> getSecondValue()
|
PLUS -> getSecondValue()
|
||||||
|
@ -113,6 +116,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lastKey = operation
|
lastKey = operation
|
||||||
lastOperation = operation
|
lastOperation = operation
|
||||||
|
@ -138,6 +142,20 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
||||||
|
// handle cases like 10+200% here
|
||||||
|
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() {
|
fun handleEquals() {
|
||||||
if (lastKey == EQUALS) {
|
if (lastKey == EQUALS) {
|
||||||
calculateResult()
|
calculateResult()
|
||||||
|
@ -203,6 +221,7 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
// handle percents manually, it doesn't seem to be possible via net.objecthunter:exp4j. "%" is used only for modulo there
|
||||||
|
// handle cases like 10%200 here
|
||||||
val result = if (sign == "%") {
|
val result = if (sign == "%") {
|
||||||
val second = secondValue / 100f
|
val second = secondValue / 100f
|
||||||
ExpressionBuilder("${baseValue.format()}*${second.format()}").build().evaluate()
|
ExpressionBuilder("${baseValue.format()}*${second.format()}").build().evaluate()
|
||||||
|
|
Loading…
Reference in New Issue