Sorry for asking such a beginner question but I can’t figure this out.
I have a long integer that I’d like to divide by 1.28 then round it up or down to the nearest integer.
long size = 24524343254;
double ratio = 1.28;
size = size * 1.28; //Error Cannot implicitly convert type 'double' to 'long'
You need to explicitly cast the double result back to long (as the compiler states – there is not implicit conversion):
You need
Math.Roundif you want to round to nearest (there is a special case for when the number to round is halfway between two numbers, then it’s rounded towards the nearest even by default). You can also simply cast the result back(long)(size * 1.28)if you only want to round towards 0.As pointed out by @CodeInChaos the implicit cast from long to double (
size * 1.28will castsizeinto a double first) can result in the loss of precision as doubles only have a precision of 53 bits but long is 64bit.