Skip to content

Instantly share code, notes, and snippets.

@sean-m
Last active June 5, 2023 21:01
Show Gist options
  • Save sean-m/98d5e9394b075b0531d3863849756404 to your computer and use it in GitHub Desktop.
Save sean-m/98d5e9394b075b0531d3863849756404 to your computer and use it in GitHub Desktop.
Just sick of looking up this size conversion math. This should be in the BCL.
/*
Usage:
var five = 5000.FromMB();
Console.WriteLine($"{five.ToKB()} KB");
Console.WriteLine($"{five.ToMB()} MB");
Console.WriteLine($"{five.ToGB()} GB");
Console.WriteLine($"{five.ToTB()} TB");
Console.WriteLine($"{five.ToPB()} PB");
Prints:
5000000 KB
5000 MB
5 GB
0.005 TB
5E-06 PB
*/
public static class StorageConverter
{
public static decimal FromBytes(this int input) => (decimal)input;
public static decimal FromBytes(this long input) => (decimal)input;
public static decimal FromBytes(this uint input) => (decimal)input;
public static decimal FromBytes(this ulong input) => (decimal)input;
public static decimal FromBytes(this float input) => (decimal)input;
public static decimal FromBytes(this double input) => (decimal)input;
public static decimal FromBytes(this decimal input) => (decimal)input;
public static decimal FromKB(this int input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this long input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this uint input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this ulong input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this float input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this double input) => (decimal)(input * Math.Pow(10,3));
public static decimal FromKB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,3));
public static decimal FromMB(this int input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this long input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this uint input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this ulong input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this float input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this double input) => (decimal)(input * Math.Pow(10,6));
public static decimal FromMB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,6));
public static decimal FromGB(this int input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this long input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this uint input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this ulong input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this float input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this double input) => (decimal)(input * Math.Pow(10,9));
public static decimal FromGB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,9));
public static decimal FromTB(this int input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this long input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this uint input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this ulong input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this float input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this double input) => (decimal)(input * Math.Pow(10,12));
public static decimal FromTB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,12));
public static decimal FromPB(this int input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this long input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this uint input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this ulong input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this float input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this double input) => (decimal)(input * Math.Pow(10,15));
public static decimal FromPB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,15));
public static decimal FromEB(this int input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this long input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this uint input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this ulong input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this float input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this double input) => (decimal)(input * Math.Pow(10,18));
public static decimal FromEB(this decimal input) => (decimal)(input * (decimal)Math.Pow(10,18));
public static double ToKB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 3));
public static double ToMB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 6));
public static double ToGB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 9));
public static double ToTB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 12));
public static double ToPB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 15));
public static double ToEB(this decimal bytes) => (double)(bytes / (decimal)Math.Pow(10, 18));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment