Below is my extension:
fun String?.toDoubleOrDefault(): Double = this?.toDoubleOrNull() ?: 0.0
when I try to apply it like this:
var a:String? = "1000"
var b:String? = "2000"
var c = a?.toDoubleOrDefault()*b?.toDoubleOrDefault()
I get this error: Type mismatch.
Required:Double
Found:Double?
Could anyone please explain what I am Doing wrong?
a.toDoubleOrDefault()*b.toDoubleOrDefault(). No need for null check since you already do so in the method. This is also the reason why you're gettingDouble?as a return value from that call.fun String?.toDoubleOrDefault(defaultVal : Double = 0.0): Double = this?.toDoubleOrNull() ?: defaultVal