I'm getting a value of type double
from an API which, in my specific case, should always return a whole number in a range well within int
's — any non-whole number will mean something went very wrong inside that API call. I do want to know when this happens.
I know I can do a post-conversion check like, let say:
double value = …;
int whole_number = (int) value;
if(value != whole_number) throw …;
Is there any shorter idiom to "Convert a double
to int
only if it is an exact conversion, and raise an exception if not"?