Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

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

1 Solution
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 ))

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now