I have a problem with dividing a long value by 1000 and round it to an integer.
My long value is: 1313179440000
My code is
long modificationtime = 1313179440000;
Math.round(modificationtime/1000l)
If i print out the divided and formated value, it returns me:
1313179392
so.
value : 1313179440000
expected: 1313179440
got : 1313179392
I do not know why this happens.
Can anybody help me?
best regards,
prdatur
Math.round(float)is being used. A float has a larger range than a long, but it cannot represent all integers within that range — in this case the integer 1313179440 (the original result of the division) lies in the part of the range that exceeds integer precision.Don’t use
Math.roundas it’s not needed (input is already an integer!), or;Use
Math.round(double), as in:Math.round(modificationTime/1000d). Note that the divisor is a double and thus the dividend (and the result) of the expression are also promoted to double.Happy coding.