mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-06-05 21:49:13 +02:00
fix #237, fixing some weird Double rounding error at plus and minus
This commit is contained in:
@@ -62,7 +62,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:178537ae0d'
|
implementation 'com.github.SimpleMobileTools:Simple-Commons:03e4d214b1'
|
||||||
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'
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import com.simplemobiletools.calculator.models.History
|
|||||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
import net.objecthunter.exp4j.ExpressionBuilder
|
import net.objecthunter.exp4j.ExpressionBuilder
|
||||||
|
import java.math.BigDecimal
|
||||||
|
|
||||||
class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
||||||
private var callback: Calculator? = calculator
|
private var callback: Calculator? = calculator
|
||||||
@@ -238,9 +239,20 @@ class CalculatorImpl(calculator: Calculator, private val context: Context) {
|
|||||||
val result = if (sign == "%") {
|
val result = if (sign == "%") {
|
||||||
val second = secondValue / 100f
|
val second = secondValue / 100f
|
||||||
ExpressionBuilder("${baseValue.format().replace(",", "")}*${second.format()}").build().evaluate()
|
ExpressionBuilder("${baseValue.format().replace(",", "")}*${second.format()}").build().evaluate()
|
||||||
|
} else {
|
||||||
|
// avoid Double rounding errors at expressions like 5250,74 + 14,98
|
||||||
|
if (sign == "+" || sign == "-") {
|
||||||
|
val first = BigDecimal.valueOf(baseValue)
|
||||||
|
val second = BigDecimal.valueOf(secondValue)
|
||||||
|
val bigDecimalResult = when (sign) {
|
||||||
|
"-" -> first.minus(second)
|
||||||
|
else -> first.plus(second)
|
||||||
|
}
|
||||||
|
bigDecimalResult.toDouble()
|
||||||
} else {
|
} else {
|
||||||
ExpressionBuilder(expression.replace(",", "")).build().evaluate()
|
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)
|
||||||
|
Reference in New Issue
Block a user