lets revert BigDecimal to Double, it was an overkill
This commit is contained in:
parent
675901410b
commit
f8f10c40ba
16 changed files with 54 additions and 81 deletions
|
@ -7,11 +7,11 @@ import android.view.Menu
|
|||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.calculator.BuildConfig
|
||||
import com.simplemobiletools.calculator.R
|
||||
import com.simplemobiletools.calculator.extensions.config
|
||||
import com.simplemobiletools.calculator.extensions.updateViewColors
|
||||
import com.simplemobiletools.calculator.helpers.*
|
||||
import com.simplemobiletools.calculator.BuildConfig
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO
|
||||
|
@ -20,7 +20,6 @@ import com.simplemobiletools.commons.models.FAQItem
|
|||
import com.simplemobiletools.commons.models.Release
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import me.grantland.widget.AutofitHelper
|
||||
import java.math.BigDecimal
|
||||
|
||||
class MainActivity : SimpleActivity(), Calculator {
|
||||
private var storedTextColor = 0
|
||||
|
@ -157,8 +156,8 @@ class MainActivity : SimpleActivity(), Calculator {
|
|||
}
|
||||
|
||||
// used only by Robolectric
|
||||
override fun setValueBigDecimal(d: BigDecimal) {
|
||||
calc.setValue(Formatter.bigDecimalToString(d))
|
||||
override fun setValueDouble(d: Double) {
|
||||
calc.setValue(Formatter.doubleToString(d))
|
||||
calc.lastKey = DIGIT
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package com.simplemobiletools.calculator.helpers
|
||||
|
||||
import android.content.Context
|
||||
import java.math.BigDecimal
|
||||
|
||||
interface Calculator {
|
||||
fun setValue(value: String, context: Context)
|
||||
|
||||
fun setValueBigDecimal(d: BigDecimal)
|
||||
fun setValueDouble(d: Double)
|
||||
|
||||
fun setFormula(value: String, context: Context)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ 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
|
||||
|
||||
class CalculatorImpl(calculator: Calculator, val context: Context) {
|
||||
var displayedNumber: String? = null
|
||||
|
@ -18,8 +17,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
|
||||
private var isFirstOperation = false
|
||||
private var resetValue = false
|
||||
private var baseValue: BigDecimal = BigDecimal.ZERO
|
||||
private var secondValue: BigDecimal = BigDecimal.ZERO
|
||||
private var baseValue = 0.0
|
||||
private var secondValue = 0.0
|
||||
private val operations = listOf("+", "-", "*", "/", "^", "%", "√")
|
||||
private var moreOperationsInRaw = false
|
||||
|
||||
|
@ -37,8 +36,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
}
|
||||
|
||||
private fun resetValues() {
|
||||
baseValue = BigDecimal.ZERO
|
||||
secondValue = BigDecimal.ZERO
|
||||
baseValue = 0.0
|
||||
secondValue = 0.0
|
||||
resetValue = false
|
||||
lastOperation = ""
|
||||
displayedNumber = ""
|
||||
|
@ -63,7 +62,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
val sign = getSign(lastOperation)
|
||||
|
||||
if (sign.isNotEmpty()) {
|
||||
if (secondValue.compareTo(BigDecimal.ZERO) == 0 && sign == "/") {
|
||||
if (secondValue == 0.0 && sign == "/") {
|
||||
context.toast(context.getString(R.string.formula_divide_by_zero_error))
|
||||
}
|
||||
|
||||
|
@ -102,16 +101,16 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
return str
|
||||
}
|
||||
|
||||
val doubleValue = Formatter.stringToBigDecimal(str)
|
||||
val doubleValue = Formatter.stringToDouble(str)
|
||||
return doubleValue.format()
|
||||
}
|
||||
|
||||
private fun updateResult(value: BigDecimal) {
|
||||
private fun updateResult(value: Double) {
|
||||
setValue(value.format())
|
||||
baseValue = value
|
||||
}
|
||||
|
||||
private fun getDisplayedNumberAsDouble() = Formatter.stringToBigDecimal(displayedNumber!!)
|
||||
private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!)
|
||||
|
||||
fun handleResult() {
|
||||
if (moreOperationsInRaw) {
|
||||
|
@ -131,7 +130,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
}
|
||||
|
||||
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("√")) {
|
||||
baseValue = 1.toBigDecimal()
|
||||
baseValue = 1.0
|
||||
}
|
||||
|
||||
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
|
||||
|
@ -206,7 +205,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
private fun handlePercent() {
|
||||
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation ?: "")
|
||||
val result = operation.getResult()
|
||||
setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue()}%")
|
||||
setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
|
||||
secondValue = result
|
||||
updateResult(result)
|
||||
inputDisplayedFormula = displayedNumber ?: ""
|
||||
|
@ -295,14 +294,14 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
|
|||
setValue(inputDisplayedFormula)
|
||||
}
|
||||
|
||||
private fun getSecondValue(): BigDecimal {
|
||||
private fun getSecondValue(): Double {
|
||||
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
|
||||
inputDisplayedFormula.substring(1)
|
||||
} else {
|
||||
inputDisplayedFormula
|
||||
}
|
||||
|
||||
return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toBigDecimal()
|
||||
return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toDouble()
|
||||
}
|
||||
|
||||
private fun zeroClicked() {
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
package com.simplemobiletools.calculator.helpers
|
||||
|
||||
import java.math.BigDecimal
|
||||
import java.text.DecimalFormat
|
||||
import java.text.DecimalFormatSymbols
|
||||
import java.util.*
|
||||
|
||||
object Formatter {
|
||||
fun bigDecimalToString(d: BigDecimal): String {
|
||||
fun doubleToString(d: Double): String {
|
||||
val symbols = DecimalFormatSymbols(Locale.US)
|
||||
symbols.decimalSeparator = '.'
|
||||
symbols.groupingSeparator = ','
|
||||
|
||||
val formatter = DecimalFormat()
|
||||
formatter.maximumFractionDigits = 50
|
||||
formatter.maximumFractionDigits = 12
|
||||
formatter.decimalFormatSymbols = symbols
|
||||
formatter.isGroupingUsed = true
|
||||
return formatter.format(d)
|
||||
}
|
||||
|
||||
fun stringToBigDecimal(str: String): BigDecimal = BigDecimal(str.replace(",", ""))
|
||||
fun stringToDouble(str: String) = str.replace(",", "").toDouble()
|
||||
}
|
||||
|
||||
fun BigDecimal.format(): String = Formatter.bigDecimalToString(this)
|
||||
fun Double.format(): String = Formatter.doubleToString(this)
|
||||
|
|
|
@ -13,7 +13,6 @@ import com.simplemobiletools.calculator.activities.MainActivity
|
|||
import com.simplemobiletools.calculator.extensions.config
|
||||
import com.simplemobiletools.commons.extensions.applyColorFilter
|
||||
import com.simplemobiletools.commons.extensions.setText
|
||||
import java.math.BigDecimal
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
||||
companion object {
|
||||
|
@ -124,7 +123,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
|
|||
}
|
||||
}
|
||||
|
||||
override fun setValueBigDecimal(d: BigDecimal) {
|
||||
override fun setValueDouble(d: Double) {
|
||||
}
|
||||
|
||||
override fun setFormula(value: String, context: Context) {
|
||||
|
|
|
@ -2,16 +2,8 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
import java.math.MathContext.DECIMAL128
|
||||
|
||||
class DivideOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class DivideOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal {
|
||||
return if (secondValue.compareTo(BigDecimal.ZERO) == 0) {
|
||||
BigDecimal.ZERO
|
||||
} else {
|
||||
baseValue.divide(secondValue, DECIMAL128)
|
||||
}
|
||||
}
|
||||
override fun getResult() = baseValue / secondValue
|
||||
}
|
||||
|
|
|
@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
|
||||
class MinusOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class MinusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal = baseValue.subtract(secondValue)
|
||||
override fun getResult() = baseValue - secondValue
|
||||
}
|
||||
|
|
|
@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
|
||||
class MultiplyOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class MultiplyOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal = baseValue.multiply(secondValue)
|
||||
override fun getResult() = baseValue * secondValue
|
||||
}
|
||||
|
|
|
@ -2,11 +2,10 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.helpers.*
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
|
||||
object OperationFactory {
|
||||
|
||||
fun forId(id: String, baseValue: BigDecimal, secondValue: BigDecimal): Operation? {
|
||||
fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
|
||||
return when (id) {
|
||||
PLUS -> PlusOperation(baseValue, secondValue)
|
||||
MINUS -> MinusOperation(baseValue, secondValue)
|
||||
|
|
|
@ -6,32 +6,29 @@ 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, val sign: String) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class PercentOperation(baseValue: Double, secondValue: Double, val sign: String) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal {
|
||||
return when {
|
||||
secondValue.compareTo(BigDecimal.ZERO) == 0 -> BigDecimal.ZERO
|
||||
sign == MULTIPLY -> {
|
||||
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32)
|
||||
baseValue.divide(partial, MathContext.DECIMAL32)
|
||||
override fun getResult(): Double {
|
||||
return when (sign) {
|
||||
MULTIPLY -> {
|
||||
val partial = 100 / secondValue
|
||||
baseValue / partial
|
||||
}
|
||||
sign == DIVIDE -> {
|
||||
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32)
|
||||
baseValue.multiply(partial, MathContext.DECIMAL32)
|
||||
DIVIDE -> {
|
||||
val partial = 100 / secondValue
|
||||
baseValue * partial
|
||||
}
|
||||
sign == PLUS -> {
|
||||
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32)
|
||||
PLUS -> {
|
||||
val partial = baseValue / (100 / secondValue)
|
||||
baseValue.plus(partial)
|
||||
}
|
||||
sign == MINUS -> {
|
||||
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32)
|
||||
MINUS -> {
|
||||
val partial = baseValue / (100 / secondValue)
|
||||
baseValue.minus(partial)
|
||||
}
|
||||
else -> {
|
||||
baseValue.divide(BigDecimal(100)).multiply(secondValue)
|
||||
baseValue / (100 * secondValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
|
||||
class PlusOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class PlusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal = baseValue.add(secondValue)
|
||||
override fun getResult() = baseValue + secondValue
|
||||
}
|
||||
|
|
|
@ -2,18 +2,17 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
import kotlin.math.pow
|
||||
|
||||
class PowerOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class PowerOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult(): BigDecimal {
|
||||
val result = baseValue.toDouble().pow(secondValue.toDouble())
|
||||
override fun getResult(): Double {
|
||||
val result = baseValue.pow(secondValue)
|
||||
|
||||
return if (java.lang.Double.isInfinite(result) || java.lang.Double.isNaN(result))
|
||||
BigDecimal.ZERO
|
||||
0.0
|
||||
else {
|
||||
BigDecimal(result)
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,9 @@ package com.simplemobiletools.calculator.operation
|
|||
|
||||
import com.simplemobiletools.calculator.operation.base.BinaryOperation
|
||||
import com.simplemobiletools.calculator.operation.base.Operation
|
||||
import java.math.BigDecimal
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class RootOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
class RootOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
|
||||
|
||||
override fun getResult() = BigDecimal(sqrt(secondValue.toDouble()).times(baseValue.toDouble()))
|
||||
override fun getResult() = sqrt(secondValue) * baseValue
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
package com.simplemobiletools.calculator.operation.base
|
||||
|
||||
import java.math.BigDecimal
|
||||
|
||||
open class BinaryOperation protected constructor(protected val baseValue: BigDecimal, protected val secondValue: BigDecimal)
|
||||
open class BinaryOperation protected constructor(protected val baseValue: Double, protected val secondValue: Double)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.simplemobiletools.calculator.operation.base
|
||||
|
||||
import java.math.BigDecimal
|
||||
|
||||
interface Operation {
|
||||
fun getResult(): BigDecimal
|
||||
fun getResult(): Double
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ import org.robolectric.annotation.Config
|
|||
import java.math.BigDecimal
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(constants = BuildConfig::class, sdk = [21])
|
||||
class MainActivityTest {
|
||||
private lateinit var activity: MainActivity
|
||||
|
||||
|
@ -164,7 +163,7 @@ class MainActivityTest {
|
|||
}
|
||||
|
||||
private fun setBigDecimal(d: BigDecimal) {
|
||||
activity.setValueBigDecimal(d)
|
||||
|
||||
}
|
||||
|
||||
private fun handleOperation(operation: String) {
|
||||
|
|
Loading…
Reference in a new issue