Skip to content

Commit

Permalink
we can now add negative numbers, like -10-5
Browse files Browse the repository at this point in the history
  • Loading branch information
Stalkerfish committed Dec 14, 2022
1 parent 32bea38 commit d09eeec
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,29 @@ class MainActivity : AppCompatActivity() {

var tvValue = tvInput?.text.toString() // convert the value of the TextView to an actual string,
// see getText() method from TextView class for further information

var prefix = ""

try{
val splitValue = tvValue.split("-") // Split the string using the operator sign as a delimiter

var one = splitValue[0] // the split string comes in arrays
var two = splitValue[1]
if(tvValue.startsWith("-")){
prefix = "-"
tvValue = tvValue.substring(1)
}
if (tvValue.contains("-")){

val splitValue = tvValue.split("-") // Split the string using the operator sign as a delimiter

var one = splitValue[0] // the split string comes in arrays
var two = splitValue[1]

if(prefix.isNotEmpty()){
one = prefix + one
}

tvInput?.text = (one.toDouble() - two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
tvInput?.text = (one.toDouble() - two.toDouble()).toString() // convert the strings to Double, do the operation,
// convert back to strings, and set to tvInput, all at the same time
}

}catch (e: ArithmeticException){
e.printStackTrace()
Expand Down

0 comments on commit d09eeec

Please sign in to comment.