mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-19 21:10:39 +01:00
fix percentage handling
This commit is contained in:
parent
a7433c4cec
commit
f011544934
@ -55,7 +55,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:cac7ba71ac'
|
implementation 'com.github.SimpleMobileTools:Simple-Commons:2f86d76a56'
|
||||||
implementation 'me.grantland:autofittextview:0.2.1'
|
implementation 'me.grantland:autofittextview:0.2.1'
|
||||||
implementation 'net.objecthunter:exp4j:0.4.8'
|
implementation 'net.objecthunter:exp4j:0.4.8'
|
||||||
}
|
}
|
||||||
|
@ -93,9 +93,6 @@ 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()
|
||||||
@ -116,7 +113,6 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
lastKey = operation
|
lastKey = operation
|
||||||
lastOperation = operation
|
lastOperation = operation
|
||||||
@ -142,19 +138,6 @@ 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
|
|
||||||
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()
|
||||||
@ -207,13 +190,26 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
if (lastOperation != "") {
|
if (lastOperation != "") {
|
||||||
val sign = getSign(lastOperation)
|
val sign = getSign(lastOperation)
|
||||||
val expression = "${baseValue.format()}$sign${secondValue.format()}".replace("√", "sqrt").replace("×", "*").replace("÷", "/")
|
val expression = "${baseValue.format()}$sign${secondValue.format()}".replace("√", "sqrt").replace("×", "*").replace("÷", "/")
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (sign == "÷" && secondValue == 0.0) {
|
if (sign == "÷" && secondValue == 0.0) {
|
||||||
context.toast(R.string.formula_divide_by_zero_error)
|
context.toast(R.string.formula_divide_by_zero_error)
|
||||||
return
|
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()) {
|
if (result.isInfinite() || result.isNaN()) {
|
||||||
context.toast(R.string.unknown_error_occurred)
|
context.toast(R.string.unknown_error_occurred)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user