C# Convert Bytes to Megabytes

Megabyte (MB)

Numbers in bytes can be represented in megabytes. This gives you a more user-friendly number to display to your user. You may also want to use this method to display the size of files.

This C# article converts numbers in bytes to megabytes with division.

Example 1

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

100000 bytes = 0.095367431640625 megabytes
1024 kilobytes = 1 megabytes
Long type

Description. 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.

Numeric Casts

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.

Suffix Examples, Numeric Suffixes

Example 2

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.

FileInfo Examples FileInfo Length, Get File Size
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

126593 bytes = 0.12 megabytes
Google search engine

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

Google search

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.

Cast Examples
.NET