0
class MainActivity : AppCompatActivity() {

    private lateinit var resultTextView: TextView


    private var operand: Double = 0.0
    private var operation: String = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        resultTextView = findViewById(R.id.resultTextView)

fun numberClick(view: View) {
       if (view is TextView) {

           val number: String = view.text.toString()
           var result: String = resultTextView.text.toString()

           if (result == "0") {
               result = ""
           }

           if (result == ".") {
               result = ""
           }
           

           resultTextView.text = result + number

       }
   }

I built a simple calculator in Kotlin but when I do anything like adding or subtracting I get ".0" in the end.

what I mean is when I do something like 2+2 it outputs 4.0 but it needs to be just 4.

1 Answers1

0

It's because it's a double, which automatically has .0 at the end

If you only input ints you can round it or cast it to int from double, so call this for example on the double to get the int without .0

.roundToInt()
Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29