kotlin android studio Unable to figure out how to covert that text to double. Can somebody correct the code please?
kotlin android studio Unable to figure out how to covert that text to double. Can somebody correct the code please?
that code gives an error -
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline fun String.toDouble(): Double defined in kotlin.text
package com.example.termoparacalculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import
com.example.termoparacalculator.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.button.setOnClickListener{
val inp = binding.input.text.toDouble()
val temp = 2.0609 * inp + 25.748
binding.textView2.text = temp
}
}
}
Solution 1:
Assuming input
is an EditText you need to do toString first
binding.input.text.toString().toDouble()
then make temp
a string too when setting since its a double
binding.textView2.text = temp.toString()