Please note the division (division) of java kotlin Int and Int

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.

Supplement

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

Please note the division (division) of java kotlin Int and Int
Java language from the perspective of Kotlin and C #
[Note] Java Output of the sum of odd and even elements
I tried to summarize the basics of kotlin and java
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
[Java / Kotlin] Resize considering the orientation of the image
CI the architecture of Java / Kotlin applications with ArchUnit
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
I summarized the types and basics of Java exceptions
[Java] [Kotlin] Generically call valueOf and values of Enum
Advantages and disadvantages of Java
I didn't understand the behavior of Java Scanner and .nextLine ().
Command to check the number and status of Java threads
Java beginners briefly summarized the behavior of Array and ArrayList
I tried JAX-RS and made a note of the procedure
[Java] I thought about the merits and uses of "interface"
Handling of java floating point [Note] while reading the reference book
[Java] Delete the elements of List
[Java] Various summaries attached to the heads of classes and members
Differences between "beginner" Java and Kotlin
About fastqc of Biocontainers and Java
[Java] Get the dates of the past Monday and Sunday in order
[Java version] The story of serialization
I want to return to the previous screen with kotlin and java!
Note on the path of request.getRequestDispatcher
This and that of the JDK
[Note] Handling of Java decimal point
Read the first 4 bytes of the Java class file and output CAFEBABE
[Java] Judgment of identity and equivalence
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
Generics of Kotlin for Java developers
The origin of Java lambda expressions
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
A note on the differences between interfaces and abstract classes in Java
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
This and that of the implementation of date judgment within the period in Java
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[Java improvement case] How to reach the limit of self-study and beyond
Find the maximum and minimum of the five numbers you entered in Java
I translated the grammar of R and Java [Updated from time to time]
Understand the difference between int and Integer and BigInteger in java and float and double
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Display Japanese calendar and days of the week using java8 standard class
Conversion between Kotlin nullable and Java Optional
Get the result of POST in Java
Relationship between kotlin and java access modifiers
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
After 3 months of Java and Spring training
Memo: [Java] Check the contents of the directory
Folding and unfolding the contents of the Recyclerview
[For beginners] Difference between Java and Kotlin
About the operation of next () and nextLine ()
[Java] Inheritance and structure of HttpServlet class