Skip to content

Commit

Permalink
Fix #196
Browse files Browse the repository at this point in the history
Fixes #196 by preventing divide by percent calculations from being messed with, and adding brackets to fix order of operations
  • Loading branch information
potatoes1286 authored Mar 23, 2023
1 parent 3e59695 commit f9cda08
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/src/main/java/com/darkempire78/opencalculator/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class Expression {
if (calculation[operatorBeforePercentPos] == '*') {
return calculation
}

if(calculation[operatorBeforePercentPos] == '/') {
// insert brackets into percentage. Fixes 900/10% -> 900/(10/100), not 900/10/100 which evals differently.
// also prevents it from doing the rest of this function, which screws the calculation up
var stringFirst = calculation.substring(0, operatorBeforePercentPos+1)
var stringMiddle = calculation.substring(operatorBeforePercentPos+1, percentPos+1)
var stringLast = calculation.substring(percentPos+1, calculation.length)
return "$stringFirst($stringMiddle)$stringLast"
}

// extract the first part of the calculation
var calculationStringFirst = calculation.subSequence(0, operatorBeforePercentPos).toString()
Expand Down Expand Up @@ -279,4 +288,4 @@ class Expression {
private fun String.addCharAtIndex(char: Char, index: Int) =
StringBuilder(this).apply { insert(index, char) }.toString()

}
}

0 comments on commit f9cda08

Please sign in to comment.