Numbers in bytes can be represented in megabytes. This gives you a more user-friendly number to display to your user.
A C# program may also use this method to display the size of files in a user interface. Kilobytes and gigabytes are also handled.
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.
FileInfo
for file sizes.float
, and this enables our numbers to be rounded off with some more precision.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
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.
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
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.
float
values in the method bodies are processed by the C# compiler before the program ever executes.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
The double
type in the C# programming language can overflow. For this reason, you can't convert huge byte
sizes with this method.
We converted between bytes, kilobytes, megabytes and gigabytes. This code is correct—it matches the results that are returned by search engines.