Home
C#
Convert Bytes to Megabytes
This page was last reviewed on Jun 22, 2021.
Dot Net Perls
Convert bytes, megabytes. Numbers in bytes can be represented in megabytes. This gives you a more user-friendly number to display to your user.
File size notes. A C# program may also use this method to display the size of files in a user interface. Kilobytes and gigabytes are also handled.
byte, sbyte
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.
Detail The methods use the long type. Long here is important because that is the type returned by FileInfo for file sizes.
long, ulong
Also Some files can have huge amounts of bytes, so a big data type (like long) is necessary.
Detail 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.
Numeric Suffix
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); } }
100000 bytes = 0.095367431640625 megabytes 1024 kilobytes = 1 megabytes
Example 2. You can use the FileInfo class to figure out the length of the file in bytes. We can then convert the file information to a formatted string in megabytes.
FileInfo
File Size
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); } }
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. This can help with displaying high-level information.
Next We look at a program that actually converts megabytes, gigabytes, and terabytes.
Note 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.
Multiply
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; } }
97.65625 0.095367431640625 1024 9.765625 1048576 10240
Notes, verification. The double type in the C# programming language can overflow. For this reason, you can't convert huge byte sizes with this method.
double
Detail The term gigabyte in the computer industry has been adopted and changed to indicate that it is equal to 1000 megabytes.
Also Scientists have introduced terms such as gibibyte and mebibyte. This fits the standard scientific prefix "kilo," which means 1000.
Summary. We converted between bytes, kilobytes, megabytes and gigabytes. This code is correct—it matches the results that are returned by search engines.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 22, 2021 (image).
Home
Changes
© 2007-2024 Sam Allen.