Problem(s) with Math.Round

I have this function and i want to return the Rounded (to nearest 1MB) of it...

private static string EstimatedFileSize(double videoBtr_Mbits, uint audioBtr_Kbits, uint video_length_sec)
        {
            // Video Bitrate = [Video Bitrate (Kilobits/Sec) + Audio Bitrate (Kilobits/Sec)]
            var videoBtr_Sum = (uint)((videoBtr_Mbits * 1024) + audioBtr_Kbits);

            // File size = Video Bitrate (Kilobits) * Video Length (Seconds) => kbytes
            var fSize_kbyte = ((videoBtr_Sum * video_length_sec) / 8) * 1024;
            
           return CommonUtility.FileSizeToString(fSize_kbyte.ToString());
        }

Open in new window

LVL 4
rotem156Asked:
Who is Participating?
 
HooKooDooKuCommented:
So what is that problem?

The one thing that looks questionable is the fact that var videoBtr_Sum is apparently already in Kbits (because you multiply a value that seems to be in Mbits by 1024 to make it Kbits), then when calculating var fSize_kbyte, you multply by 1024 again (and I don't know what the /8 is for).

Perhaps var fSize_kbyte should really be fSize_byte (then the * 1024 would make sense).

To get fSize_byte rounded to the nearest 1MB, simply add 1/2 M and divide by 1M...
fSize_MByte = (uint)(( fSize_byte + 512 * 1024 ) / ( 1024 * 1024 ))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.