Convert bytes, megabytes. 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. 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.

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.

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.

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.

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.

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.