You could also consider using decimal
.
size = (long)Math.Round(size * 1.28m);//note the m making the literal a decimal.
This offers two advantages over double
in this situation:
decimal
can represent every long
value exactly (Unlike double
which only has 53 mantissa bits, decimal has >90 mantissa bit, with is enough to represent the 63 bits used by long
)
- it can represent 1.28 exactly.
You should also make sure, that MidpointRounding.ToEven
is fine for you. It rounds 1.5
to 2
, but 0.5
to 0
.
When using Decimal
casting to long
if the value is outside the range of long
will cause an exception.
When using double
overflow behavior depends on the mode. If you're in a checked
section, it will throw an exception, in an unchecked
context it will result in an undefined value. Since undefined values are rarely desirable, I recommend using checked
with double
:
size = checked((long)Math.Round(size * 1.28));