I was refactoring some old code and came across the following line of code to convert bytes to GB.
decimal GB = KB / 1024 / 1024 / 1024;
Is
If exact precision is not important, use double:
double gb = kb / 1048576D
Agree with Pavel here - there's not really any need to refactor this code... in fact, if this is the biggest problem in your codebase, I think you might be sitting on the most well-written software ever.
I needed it the other way around, convert from 3rd party component literal size in words (e.g. "0 bytes", "1.1 MB") into generic size in bytes. so I used it this way:
private static long UnformatBytes(string sizeInWords)
{
if(string.IsNullOrWhiteSpace(sizeInWords))
return -1;
string size = sizeInWords.Split(' ').FirstOrDefault();
double result;
if (string.IsNullOrWhiteSpace(size) || !double.TryParse(size, out result))
{
Debugger.Break();
return -1;
}
int pow;
if (sizeInWords.IndexOf("byte", StringComparison.OrdinalIgnoreCase) > -1)
pow = 0;
else if (sizeInWords.IndexOf("kb", StringComparison.OrdinalIgnoreCase) > -1)
pow = 1;
else if (sizeInWords.IndexOf("mb", StringComparison.OrdinalIgnoreCase) > -1)
pow = 2;
else if (sizeInWords.IndexOf("gb", StringComparison.OrdinalIgnoreCase) > -1)
pow = 3;
else if (sizeInWords.IndexOf("tb", StringComparison.OrdinalIgnoreCase) > -1)
pow = 4;
else
return -1;
return System.Convert.ToInt64((result * Math.Pow(1024, pow)));
}
Personally I'd write it like this: decimal GB = KB / (1024 * 1024);
but there's really no need to refactor the code as written.
In theory, this is faster (precomputing the constant to do multiplication instead of division). It's probably not used often enough to matter, but just in case.
double const KbToGbFactor = 1d / 1024 /1024;
double gb = kb * KbToGbFactor;
/// <summary>
/// Function to convert the given bytes to either Kilobyte, Megabyte, or Gigabyte
/// </summary>
/// <param name="bytes">Double -> Total bytes to be converted</param>
/// <param name="type">String -> Type of conversion to perform</param>
/// <returns>Int32 -> Converted bytes</returns>
/// <remarks></remarks>
public static double ConvertSize(double bytes, string type)
{
try
{
const int CONVERSION_VALUE = 1024;
//determine what conversion they want
switch (type)
{
case "BY":
//convert to bytes (default)
return bytes;
case "KB":
//convert to kilobytes
return (bytes / CONVERSION_VALUE);
case "MB":
//convert to megabytes
return (bytes / CalculateSquare(CONVERSION_VALUE));
case "GB":
//convert to gigabytes
return (bytes / CalculateCube(CONVERSION_VALUE));
default:
//default
return bytes;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
/// <summary>
/// Function to calculate the square of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be squared</param>
/// <returns>Double -> THe provided number squared</returns>
/// <remarks></remarks>
public static double CalculateSquare(Int32 number)
{
return Math.Pow(number, 2);
}
/// <summary>
/// Function to calculate the cube of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be cubed</param>
/// <returns>Double -> THe provided number cubed</returns>
/// <remarks></remarks>
public static double CalculateCube(Int32 number)
{
return Math.Pow(number, 3);
}
//Sample Useage
String Size = "File is " + ConvertSize(250222,"MB") + " Megabytes in size"
The original code is succinct, easy to read, and with reasonable variable names, self-documenting; I wouldn't change it.
If you absolutely must refactor, you could create a set of extension methods on the numeric types:
public static double BytesToKilobytes(this Int32 bytes)
{
return bytes / 1024d;
}
public static double BytesToMegabytes(this Int32 bytes)
{
return bytes / 1024d / 1024d;
}
public static double KilobytesToBytes(this double kilobytes)
{
return kilobytes * 1024d;
}
//You can then do something like:
double filesize = 32.5d;
double bytes = filesize.KilobytesToBytes();
But unless your code does virtually nothing but convert bytes to kilobytes etc, all this will really do is clutter up Intellisense for no real gain.