Why did you get hooked on such an easy thing? A memorandum so that no one is addicted to wasting time like me
I expected the following to be 0.25 as a matter of course ...
var rate: Double = 0.0
rate = 1 / 4
Log.v("TEST:", "rate:${rate}")
I get the following compilation error.
Type mismatch: inferred type is Int but Double was expected
Google Translate: "Type mismatch: Estimated type is Int but Double is required"
Wai knows that! !! !!
Hmm? Ah Σ (゚ д ゚;) ,,,, Is that so? Since it is a calculation of Int and Int, the type judgment of the calculation result was Int ... I didn't notice why. .. ..
So the solution. Either or both can be matched to Double.
val num1 = 1.toDouble()
val num2 = 4.toDouble()
val aaa = num1 / num2
Log.v("TEST:", "rate = ${aaa}")
V/TEST:: rate = 0.25
Yes. The result was as expected. I can't stop crying because of my illness.
By the way ...
val aaa = (1/4).toDouble()
Log.v("TEST:", "rate = ${aaa}")
V/TEST:: rate = 0.0
If you do this, the calculation result will come in Int and it will be like converting it to Double.
The expected result is not returned.
Note that if it is 10/4
, it will be 2.0
.
Then, have a good kotlin life!
Recommended Posts