C# Convert Gigabytes and Megabytes

Megabyte (MB)

You have a number that is in the form of gigabytes, terabytes or megabytes and want to convert it to another unit scale. For example, you want to convert a number in megabytes to gigabytes for displaying high-level information about a program or data set, using the C# language.

Example

First here 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 and the constant multiplications will cause no performance loss.

This C# example converts numbers in megabytes to gigabytes with division.

Methods that convert gigabytes [C#]

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 terabyte
	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
Main method

Overview. This is a C# 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. The Convert methods then perform a computation (division or multiplication) and push that value onto the evaluation stack, where it is then returned to the calling method for passing to the Console.WriteLine method.

Verify results

Double keyword

The double type in the C# programming language can overflow and cannot contain extremely huge or small numbers accurately. 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. Additionally, some scientists have introduced terms such as gibibyte and mebibyte, which may be more correct when reconciling the standard scientific prefix 'kilo', which actually means 1000. I do not recommend using the term 'kibibyte' as very few people are familiar with it.

Summary

The C# programming language

We looked at how you can convert figures in units of megabytes, gigabytes, and terabytes to figures in other units. Computers cover trillions of bytes in some cases and for displaying these numbers to programmers or consumers, more concise units are best. We performed these conversions and then noted some possible problems are related issues to converting byte figures.

Convert Bytes to Megabytes Cast Examples
.NET