I am trying to take a file and split it into 512kb chunks. To calculate the number of chunks, I need to do some basic math. For some reason, I am having some data loss issues. I can’t figure out what I’m doing wrong. I currently have:
int chunkSize = 524288; // 512kb
int fileByteCount = GetFileSizeInBytes();
decimal result = ((decimal)(fileByteCount)) / ((decimal)(chunkSize));
int packetCount = Math.Ceiling(result); // Doesn't work.
I can’t use Math.Ceiling because it requires a double. But, I think, I need to use a decimal to do the math. What am I doing wrong? How do I do this basic math operation?
Use integer math:
Note that a file size should really be long, transferring files larger than 2 gigabytes is not unusual.