when i use this: 9 / 5 * celsius + 32; - i get as result 73.0
but when i use this: 9 / 5.0 * celsius + 32; - i get as result 105.8 (the right answer)
why is the .0 so important i dont understand?
Can someone explain me this... .0
In der Diskussion
Kommentare (2)
- Beliebt
- Neu
- Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Gellert Varga
14 September 2021, 13:30
In Java, integers in code are of type int by default.
If two int types are divided, the result is also just an int type. So you don't get an exact result:
5 / 3 == 1; // int 1 and not 1.6666
7 / 3 == 2; // int 2 and not 2.3333
etc.
If you want to get an exact value, that means you want a double type as the result, like 1.6666.
But to do that, at least one of the two starting numbers ( 5 and 3 ) must be of type double, which can be done this way:
5 / 3.0
7.0 / 3
+4
Ayla Hille
19 Februar 2022, 14:57
Thank you! That explanation helped a lot!
+1