A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

40 lines
1.0 KiB

using System;
namespace Avalonia.Dialogs
{
internal static class ByteSizeHelper
{
private const string formatTemplate = "{0}{1:0.#} {2}";
private static readonly string[] Prefixes =
{
"B",
"KB",
"MB",
"GB",
"TB",
"PB",
"EB",
"ZB",
"YB"
};
public static string ToString(ulong bytes)
{
if (bytes == 0)
{
return string.Format(formatTemplate, null, 0, Prefixes[0]);
}
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]);
}
}
}