Is it safe to cast an Integer to Double and back again?
To be a bit more specific, is it true for all integers that originalValue
equals retrievedValue
in the following scenario:
int originalValue = 42;
double backingField = (double)originalValue;
int retrievedValue = (int)backingField;
A bit of background/motivation: I am writing lots of micro services in C# with lots of interfaces. For a time interval in seconds, some clients send them as integers, others as floating values. I need to store them and return the same value as the client sent me. My question targets, if it is safe to assume, that when I retrieve an integer, but store all values as double that a cast to integer on the retrieving client will always return the original value. Especially as casting is done by truncating any values after the comma.
Can any cast to double result in a value lower than the integer, e.g. 42 -> 41.9999999999999?