public static void Main()
Console.WriteLine("FileSize: {0}\t->\t{1}", val, FileSizeHelper.GetSize(val));
public static class FileSizeHelper
private const decimal KB = 1024M;
private const decimal MB = 1048576M;
private const decimal GB = 1073741824M;
private const decimal TB = 1099511627776M;
public static string GetSize(long bytesSize)
if(bytesSize <= 0) { return "0 B"; }
if (bytesSize >= TB) { size = bytesSize / TB; suffix = "TB"; }
else if (bytesSize >= GB) { size = bytesSize / GB; suffix = "GB"; }
else if (bytesSize >= MB) { size = bytesSize / MB; suffix = "MB"; }
else if (bytesSize >= KB) { size = bytesSize / KB; suffix = "KB"; }
else { size = bytesSize; suffix = "B"; }
size = PrecisionHelper.Truncate(size, 2);
return $"{size} {suffix}";
public static class PrecisionHelper
public static decimal Truncate(decimal value, int precision)
decimal step = (decimal)Math.Pow(10, precision);
decimal temp = Math.Truncate(step * value);
public static double Truncate(double value, int precision)
double step = Math.Pow(10, precision);
double temp = Math.Truncate(step * value);
public static double PrecisionStep(int precision)
double step = Math.Pow(10, precision);