Skip to content

Commit

Permalink
Merge pull request #252 from potatoes1286/patch-1
Browse files Browse the repository at this point in the history
Fix #196
  • Loading branch information
Darkempire78 authored Mar 24, 2023
2 parents ccb1f0e + f9cda08 commit e9aaf21
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 e9aaf21

Please sign in to comment.