Problem(s) with Math.Round

Posted on 2012-08-22
Last Modified: 2016-09-29
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

Question by:rotem156
    1 Comment
    LVL 16

    Accepted 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 ))

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now