C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Long: The methods use the long type. Long here is important because that is the type returned by FileInfo for file sizes.
LongNumeric CastsAlso: Some files can have huge amounts of bytes, so a big data type (like long) 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.
SuffixC# 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
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.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);
}
}
Output
126593 bytes = 0.12 megabytes
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.
MultiplyC# 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
Then: The Convert methods perform a computation (division or multiplication) and push that value onto the evaluation stack.
Finally: The value is then returned to the calling method for passing to the Console.WriteLine method.
ConsoleAlso: Scientists have introduced terms such as gibibyte and mebibyte. This fits the standard scientific prefix "kilo," which means 1000.