简体   繁体   中英

Kotlin Big Decimal division returning inaccurate results

I am building a calculator with Kotlin and android studio (my first project). All equations were working properly until recent tweaks with decimal format. Now large division operations returns inaccurate results.

For example 99,999,999 / 9% (0.09) = 1.11111110000E9 But 9% / 99,999,999 = 0 when it should = 9.0000000009E-10

Is this because the current DecimalFormat cannot return the negative exponent?

EDIT: after more testing I've found that changing division method to

doubleNum = doubleNum.divide(numBigDecimal, 10, BigDecimal.ROUND_HALF_UP).stripTrailingZeros()

running the same equation will return 9E-10 before formatting. After decimal format the result shows as "." only with no digits

// enum class and class properties
enum class LogicTypes {
    None,Add,Subtract,Multiply,Divide
}

class MainActivity : AppCompatActivity() {

    private var logicActive = false
    private var currentLogic = LogicTypes.None
    private var currentNum = BigDecimal.ZERO
    private var displayNum = ""
    private var memoryNum = BigDecimal.ZERO
// add num function - buttons 0-9 send indices to num arg
fun addNum(num: BigDecimal) {
        val inputNum = num.toString()
        if (displayNum == "0" && inputNum == "0") {
            return
        }
        if (displayNum.contains(".")) {
            val stringForm = displayNum.substring(displayNum.indexOf('.'), displayNum.length)
            if (stringForm.length > 10) {
                clearCalc()
                Toast.makeText(this, "Only 10 digits after decimal point allowed.", Toast.LENGTH_SHORT).show()
                return
            }
        }
        if (displayNum.length >= 15 && !displayNum.contains(".")) {
            clearCalc()
            Toast.makeText(this, "Maximum of 15 digits allowed.", Toast.LENGTH_SHORT).show()
            return
        }
        if (inputNum == "0" && currentNum.toDouble() == 0.0 && displayNum.contains(".")) {
            if (displayNum.length > 11) {
                clearCalc()
                Toast.makeText(this, "Only 10 digits after decimal point allowed.", Toast.LENGTH_SHORT).show()
                return
            }
            displayNum = "$displayNum$inputNum"
            textView.text = displayNum
            return
        }
        if (logicActive) {
            logicActive = false
            displayNum = "0"
        }

        displayNum = "$displayNum$inputNum"
        updateDisplayNum()
    }
// set currentNum and send to numFormat function to update textView
fun updateDisplayNum() {
        if (currentNum.toString().length > 15) {
            clearCalc()
            Toast.makeText(this, "Maximum of 15 digits allowed.", Toast.LENGTH_SHORT).show()
            return
        }

        val numBigDecimal = displayNum.toBigDecimal()

        if(currentLogic == LogicTypes.None) {
            if(displayNum.contains("-") && currentNum == BigDecimal.ZERO) {
                textView.text = displayNum
                return
            } else {
                currentNum = numBigDecimal
            }
        }

        numFormat()
    }
// format decimal and integers and update textview with exponent
fun numFormat() {
        val numBigDecimal = displayNum.toBigDecimal()
        if(displayNum.contains(".")) {
            val stringForm = displayNum.substring(displayNum.indexOf('.'), displayNum.length)
            var numFormat = "#,##0."

            if(stringForm.length > 1) {
                for (num in stringForm.indices-1) {
                    numFormat += "0"
                }
            }

            if (displayNum.length > 16 || stringForm.length > 9) {
                // stringform length > 9 works for division result - anything higher returns trailing zeros.
                    // anything under 11 for stringform condition results in inaccurate input -
                        // adding decimal through addNum() will return Exponential notation before logic
                            // I only want E notation on result only- have yet to test other equations - 
                                // this can also make it impossible to take the result and conduct another logic operation as the result appends E0 
                                  // and thus the trailing digits after decimal is greater than 10
                    numFormat = "0.0000000000E0"
            }

            val df = DecimalFormat(numFormat)
            textView.text = df.format(numBigDecimal)
            return
        }
        var df = DecimalFormat("#,###")

        if (displayNum.length > 15) {
            df = DecimalFormat("0.0000000000E0")
        }

        textView.text = df.format(numBigDecimal)
    }
// change logic to enum mode when button operator pressed
fun changeLogic(mode: LogicTypes) {
        currentLogic = mode
        logicActive = true
    }

// calculate function
fun calculate() {
        if (logicActive || currentLogic == LogicTypes.Divide && displayNum.toBigDecimal() == BigDecimal.ZERO
            || currentNum == BigDecimal.ZERO) {
            Log.i(LOG_TAG, "caught the zero")
            return
        }
        val numBigDecimal = displayNum.toBigDecimal()
        var doubleNum = currentNum


        val currentNumString = doubleNum.stripTrailingZeros().toPlainString()
        val numBigDecimalString = numBigDecimal.stripTrailingZeros().toPlainString()

        val addMsg = getString(R.string.calc_message, currentNumString, "+", numBigDecimalString)
        val subMsg = getString(R.string.calc_message, currentNumString, "-", numBigDecimalString)
        val multiMsg = getString(R.string.calc_message, currentNumString, "*", numBigDecimalString)
        val divMsg = getString(R.string.calc_message, currentNumString, "/", numBigDecimalString)


        when(currentLogic) {
            LogicTypes.Add -> {
                hintView.text = addMsg
                doubleNum += numBigDecimal
                doubleNum = doubleNum.stripTrailingZeros()
            }
            LogicTypes.Subtract -> {
                hintView.text = subMsg
                doubleNum -= numBigDecimal
                doubleNum = doubleNum.stripTrailingZeros()
            }
            LogicTypes.Multiply -> {
                hintView.text = multiMsg
                doubleNum *= numBigDecimal
                doubleNum = doubleNum.stripTrailingZeros()
            }
            LogicTypes.Divide -> {
                hintView.text = divMsg
                doubleNum /= numBigDecimal
                doubleNum = doubleNum.stripTrailingZeros()
            }
            LogicTypes.None -> return
        }

        currentLogic = LogicTypes.None
        displayNum = doubleNum.toString()
        updateDisplayNum()
        logicActive = true
    }

Ok the issue was that I was using this in the calculate function.

displayNum = doubleNum.toString()

Changing to.toPlainString() gives correct notations. There are still issues with formatting but I'll see if I can work those out on my own

EDIT : I solved the formatting issue in the numFormat by creating a boolean property, setting it to true in the calculation function, and passing it to the numFormat condition:

if (displayNum.length > 16 || stringForm.length > 9 && resultActive) {
      numFormat = "0.0000000000E0"
      resultActive = false
   }

This way the format only applies to calculated numbers

I also passed it to the addNum function for calculations made after the first calculation

if(resultActive) {
      resultActive = false
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM