C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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. Kilobytes and gigabytes are also handled.
Example. First 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. We also convert kilobytes to megabytes.
Based on:
.NET 4
C# program that converts bytes and kilobytes
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


In this example, the methods use the 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.
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.
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". Use FileInfo to get information about it and then convert that to a formatted string in megabytes.
Tip: Check Google's conversion figure by typing "convert 126593 bytes to megabytes".
C# program that uses FileInfo
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");
// 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

Example 3. We have a number that is in the form of gigabytes, terabytes or megabytes. We want to convert it to another unit scale. Gigabytes are useful for displaying high-level information about a program or data set.
Next, we look at a program that actually converts megabytes, gigabytes, and terabytes. Note that the constant float values in the method bodies are processed by the C# compiler before the program ever executes.
So: The constant multiplications will cause no performance loss in this program at runtime.
C# program that converts gigabytes
using System;
class Program
{
static void Main()
{
Console.WriteLine(ConvertMegabytesToGigabytes(100000));
Console.WriteLine(ConvertMegabytesToTerabytes(100000));
Console.WriteLine(ConvertGigabytesToMegabytes(1));
Console.WriteLine(ConvertGigabytesToTerabytes(10000));
Console.WriteLine(ConvertTerabytesToMegabytes(1));
Console.WriteLine(ConvertTerabytesToGigabytes(10));
}
static double ConvertMegabytesToGigabytes(double megabytes) // SMALLER
{
// 1024 megabyte in a gigabyte
return megabytes / 1024.0;
}
static double ConvertMegabytesToTerabytes(double megabytes) // SMALLER
{
// 1024 * 1024 megabytes in a terabyte
return megabytes / (1024.0 * 1024.0);
}
static double ConvertGigabytesToMegabytes(double gigabytes) // BIGGER
{
// 1024 gigabytes in a terabyte
return gigabytes * 1024.0;
}
static double ConvertGigabytesToTerabytes(double gigabytes) // SMALLER
{
// 1024 gigabytes in a terabyte
return gigabytes / 1024.0;
}
static double ConvertTerabytesToMegabytes(double terabytes) // BIGGER
{
// 1024 * 1024 megabytes in a terabyte
return terabytes * (1024.0 * 1024.0);
}
static double ConvertTerabytesToGigabytes(double terabytes) // BIGGER
{
// 1024 gigabytes in a terabyte
return terabytes * 1024.0;
}
}
Output
97.65625
0.095367431640625
1024
9.765625
1048576
10240

This program that defines the Main method entry point and then begins execution of six methods. It loads six numeric constants onto the stack and passes them to the Convert methods defined in the Program class.
Then, the Convert methods perform a computation (division or multiplication) and push that value onto the evaluation stack. The value is then returned to the calling method for passing to the Console.WriteLine method.
Verify results. The double type in the C# programming language can overflow and cannot contain extremely huge or small numbers. For this reason, you can't convert huge byte sizes with this method.
The term gigabyte in the computer industry has been adopted and changed by marketers to indicate that it is equal to 1000 megabytes. Mainly hard drive manufacturers are responsible for propagating this.
Also: Scientists have introduced terms such as gibibyte and mebibyte. This fits the standard scientific prefix "kilo," which means 1000.
But: I do not recommend using the term "kibibyte" as very few people are familiar with it.
Summary. We converted between bytes, kilobytes, megabytes and gigabytes. This code is correct. It matches the results that Google gives. Of course, you don't want to use Google to perform such a simple operation. This code is far faster.