C# Convert Bytes to Megabytes

by Sam Allen - Updated January 8, 2010
MB, megabytes

You want to convert a number in bytes to megabytes. This gives you a more user-friendly number to display to your user. You also want to use this method to display the size of files. Here we use the C# language to convert figures in bytes to megabytes and display the results.

Megabytes and bytes

Here we see a static method that converts a number in bytes to megabytes. This helps with user interfaces and communicating to your users a more readable file size. Additionally, we convert kilobytes to megabytes.

=== Program that converts bytes and kilobytes (C#) ===

using System;

class Program
{
    static double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }

    static double ConvertKilobytesToMegabytes(long kilobytes)
    {
        return kilobytes / 1024f;
    }

    static void Main()
    {
        // Convert bytes to megabytes.
        double megabytes1 = ConvertBytesToMegabytes(100000L);

        // Write the result.
        Console.WriteLine("{0} bytes = {1} megabytes",
            100000L,
            megabytes1);

        // Convert kilobytes to megabytes.
        double megabytes2 = ConvertKilobytesToMegabytes(1024L);

        // Write the result.
        Console.WriteLine("{0} kilobytes = {1} megabytes",
            1024L,
            megabytes2);
    }
}

=== Output of the program ===

100000 bytes = 0.095367431640625 megabytes
1024 kilobytes = 1 megabytes

Description of the methods. The methods use long type. Long here is important because that is the type returned by FileInfo for file sizes. Also, some files can have huge amounts of bytes, so a big data type is necessary.

(See Numeric Casts and Numbers.)

Description of the numeric suffixes. An F is used at the end. The F stands for float, and this enables our numbers to be rounded off with some more precision.

Using the methods with files

You can use the FileInfo class to figure out the length of the file in bytes. Let's say that you have the file "Anthem.html". Use FileInfo to get information about it and then convert that to a formatted string in megabytes.

(See File Size With FileInfo.)

=== Program that uses FileInfo (C#) ===

using System;
using System.IO;

class Program
{
    static double ConvertBytesToMegabytes(long bytes)
    {
        return (bytes / 1024f) / 1024f;
    }

    static void Main()
    {
        // Get the file information.
        FileInfo info = new FileInfo("Anthem.html");

        // Now convert to a string in megabytes.
        string s = ConvertBytesToMegabytes(info.Length).ToString("0.00");

        // Convert bytes to megabytes.
        Console.WriteLine("{0} bytes = {1} megabytes",
            info.Length,
            s);
    }
}

=== Output of the program ===

126593 bytes = 0.12 megabytes

Making sure the results are correct. Check Google's conversion figure by typing "convert 126593 bytes to megabytes". Alternatively, use the link here to check the results.

(Visit www.google.com.)

Summary

Here we looked at ways you can convert between bytes, kilobytes and megabytes using the C# language. This code is correct, as it matches the results that Google gives. Of course, you don't want to use Google to perform such a simple operation, and this code is far faster for .NET programs.

(Do not copy this page.)

Dot Net Perls | Search
Conversion | Convert Bool to Int | Convert Char Array to String | Convert List to Array | Convert String Array to String | List and String Conversion
C# | Decrement Int | HTML Brackets | Parameter Optimization Tip | SaveFileDialog Tutorial
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen