巧用Log函数获取image文件大小

byte[] data = image.data;

SizeSuffix(data.length);

static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
static string SizeSuffix(long value)
{
if (value < 0) { return "-" + SizeSuffix(-value); }
if (value == 0) { return "0.0 bytes"; }

int mag = (int)Math.Log(value, 1024);
decimal adjustedSize = (decimal)value / (1L << (mag * 10));

return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
}

这个里面用到的数学函数Log,非常简洁,不能太赞。

http://stackoverflow.com/questions/14488796/does-net-provide-an-easy-way-convert-bytes-to-kb-mb-gb-etc/#answer-14488941

原文地址:https://www.cnblogs.com/yoyohappy/p/4341497.html