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?
The ceil() function returns the integer as a double value.
C ceil() The ceil() function computes the nearest integer greater than the argument passed.
The Math. ceil() function always rounds up and returns the smaller integer greater than or equal to a given number.
Use integer math:
int chunkSize = 524288; // 512kb
int fileByteCount = GetFileSizeInBytes();
int packetCount = (fileByteCount + chunkSize - 1) / chunkSize;
Note that a file size should really be long, transferring files larger than 2 gigabytes is not unusual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With