calculate the percent change of two uint32 numbers in c closed

Assuming the basic shape of the expression is correct (so, you want to express the change as a percentage of UInt32.MaxValue, not as a percentage of the old or new value), the problem is that multiplying the difference by 100 overflows. It could be fixed by temporarily using an ulong like this:

uint OriginInt=32654, NewInt=4256321445;
(uint)(((ulong)NewInt - OriginInt) * 100 / uint.MaxValue);

The result is 99.

Of course since this is still integer arithmetic, it will still be the case that the result is rounded down, so a small percentages (below 1) would still result in zero.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top