mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-03-11 00:40:08 +01:00
improve percentage handling
This commit is contained in:
parent
6db8017611
commit
13829b852a
@ -3,6 +3,7 @@ package com.simplemobiletools.calculator.helpers
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calculator.R
|
||||
import com.simplemobiletools.calculator.operation.OperationFactory
|
||||
import com.simplemobiletools.calculator.operation.PercentOperation
|
||||
import com.simplemobiletools.commons.extensions.areDigitsOnly
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import java.math.BigDecimal
|
||||
@ -203,12 +204,12 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
}
|
||||
|
||||
private fun handlePercent() {
|
||||
OperationFactory.forId(PERCENT, baseValue, getDisplayedNumberAsDouble())?.let {
|
||||
val result = it.getResult()
|
||||
setFormula("${baseValue.format()}${getSign(lastOperation)}${getDisplayedNumberAsDouble().format()}%")
|
||||
secondValue = result
|
||||
calculateResult(false)
|
||||
}
|
||||
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation ?: "")
|
||||
val result = operation.getResult()
|
||||
setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue()}%")
|
||||
secondValue = result
|
||||
updateResult(result)
|
||||
inputDisplayedFormula = displayedNumber ?: ""
|
||||
}
|
||||
|
||||
fun handleClear() {
|
||||
@ -294,6 +295,16 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
setValue(inputDisplayedFormula)
|
||||
}
|
||||
|
||||
private fun getSecondValue(): BigDecimal {
|
||||
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
|
||||
inputDisplayedFormula.substring(1)
|
||||
} else {
|
||||
inputDisplayedFormula
|
||||
}
|
||||
|
||||
return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toBigDecimal()
|
||||
}
|
||||
|
||||
private fun zeroClicked() {
|
||||
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
|
||||
inputDisplayedFormula.substring(1)
|
||||
|
@ -12,7 +12,6 @@ object OperationFactory {
|
||||
MINUS -> MinusOperation(baseValue, secondValue)
|
||||
DIVIDE -> DivideOperation(baseValue, secondValue)
|
||||
MULTIPLY -> MultiplyOperation(baseValue, secondValue)
|
||||
PERCENT -> PercentOperation(baseValue, secondValue)
|
||||
POWER -> PowerOperation(baseValue, secondValue)
|
||||
ROOT -> RootOperation(baseValue, secondValue)
|
||||
else -> null
|
||||
|
@ -1,16 +1,38 @@
|
||||
package com.simplemobiletools.calculator.operation
|
||||
|
||||
import com.simplemobiletools.calculator.helpers.DIVIDE
|
||||
import com.simplemobiletools.calculator.helpers.MINUS
|
||||
import com.simplemobiletools.calculator.helpers.MULTIPLY
|
||||
import com.simplemobiletools.calculator.helpers.PLUS
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
import java.math.MathContext
|
||||
|
||||
class PercentOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class PercentOperation(baseValue: BigDecimal, secondValue: BigDecimal, val sign: String) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal {
|
||||
return if (secondValue.compareTo(BigDecimal.ZERO) == 0) {
|
||||
BigDecimal.ZERO
|
||||
} else {
|
||||
baseValue.divide(BigDecimal(100)).multiply(secondValue)
|
||||
return when {
|
||||
secondValue.compareTo(BigDecimal.ZERO) == 0 -> BigDecimal.ZERO
|
||||
sign == MULTIPLY -> {
|
||||
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32)
|
||||
baseValue.divide(partial, MathContext.DECIMAL32)
|
||||
}
|
||||
sign == DIVIDE -> {
|
||||
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32)
|
||||
baseValue.multiply(partial, MathContext.DECIMAL32)
|
||||
}
|
||||
sign == PLUS -> {
|
||||
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32)
|
||||
baseValue.plus(partial)
|
||||
}
|
||||
sign == MINUS -> {
|
||||
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32)
|
||||
baseValue.minus(partial)
|
||||
}
|
||||
else -> {
|
||||
baseValue.divide(BigDecimal(100)).multiply(secondValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user