3 changed files with 24 additions and 35 deletions
@ -1,25 +1,40 @@ |
|||||
|
using System; |
||||
|
|
||||
namespace Avalonia.Dialogs |
namespace Avalonia.Dialogs |
||||
{ |
{ |
||||
internal static class ByteSizeHelper |
internal static class ByteSizeHelper |
||||
{ |
{ |
||||
|
private const string formatTemplate = "{0}{1:0.#} {2}"; |
||||
|
|
||||
private static readonly string[] Prefixes = |
private static readonly string[] Prefixes = |
||||
{ |
{ |
||||
"B", |
"B", |
||||
"KB", |
"KB", |
||||
"MB", |
"MB", |
||||
"GB", |
"GB", |
||||
"TB" |
"TB", |
||||
|
"PB", |
||||
|
"EB", |
||||
|
"ZB", |
||||
|
"YB" |
||||
}; |
}; |
||||
|
|
||||
public static string ToString(long bytes) |
public static string ToString(ulong bytes) |
||||
{ |
{ |
||||
var index = 0; |
if (bytes == 0) |
||||
while (bytes >= 1000) |
|
||||
{ |
{ |
||||
bytes /= 1000; |
return string.Format(formatTemplate, null, 0, Prefixes[0]); |
||||
++index; |
|
||||
} |
} |
||||
return $"{bytes:N} {Prefixes[index]}"; |
|
||||
|
var absSize = Math.Abs((double)bytes); |
||||
|
var fpPower = Math.Log(absSize, 1000); |
||||
|
var intPower = (int)fpPower; |
||||
|
var iUnit = intPower >= Prefixes.Length |
||||
|
? Prefixes.Length - 1 |
||||
|
: intPower; |
||||
|
var normSize = absSize / Math.Pow(1000, iUnit); |
||||
|
|
||||
|
return string.Format(formatTemplate,bytes < 0 ? "-" : null, normSize, Prefixes[iUnit]); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue