#lossy conversion from double to int
10 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @grave estuary! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Cast it.
Like
int someInt = (int) someDouble;
Of course, the error is there for a reason. If you do cast it like I said, you might lose the part of the value that doesn't fit in an int, duh
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
Mind you, this code will still compile. It's merely a warning. Warnings can be ignored if you want to, you could just supress them with an annotation. For more information on how floating point values work, check out the documentation for the IEEE 754 spec.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
What I said about lossy conversion was a warning. But if you do the cast as I showed, then there would be no warning, because by writing it so, as a programmer you explicitly signify your intention to perform a type conversion that isn't just done implicitly. You're supposed to do that only if you know what it means to write it, that it converts in a way that may be lossy.
The original problem mentioned, that looked like int someInt = someDouble; is very much an error, not merely a warning. Java doesn't allow implicit conversions that may be lossy, except the more complicated cases of int to float and long to double.
Those last ones are lossy and I hate it. Int to double is fine though
Same with math round returning int or long