1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/// <summary>
/// Calculates a good Looking file size
/// </summary>
/// <param name="size">Your size in Bytes</param>
/// <returns>String, value not greater 1024, with unit</returns>
public static String MakeNiceSize(double size)
{
return MakeNiceSize(size, "auto");
}
/// <summary>
/// Calculates a good Looking file size
/// </summary>
/// <param name="size">Your size in Bytes</param>
/// <param name="mode">Any of "auto","B","KB","MB","GB","TB","PB","EB"</param>
/// <returns>String, value with unit</returns>
public static String MakeNiceSize(double size, string mode)
{
string[] Suffix = new string[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"};
int run = 0;
if (mode=="auto")
{
while (size >= 1024)
{
size /=1024;
run++;
}
}
else if(mode != "auto")
{
if (Suffix.Contains(mode))
{
while (Suffix[run] != mode)
{
size /=1024;
run++;
}
}
else
{
return "ERROR: Unknown mode";
}
}
return Math.Round(size,2).ToString("00.00") + " " + Suffix[run];
}
|