Sprache: C#
Das folgende Snippet gibt eine eine Anzahl von Bytes in der höchsten darstellbaren Einheit (KB, MB, GB, …) aus.
public string ToFuzzyByteString(long bytes)
{
double s = bytes;
string[] format = new string[]
{
"{0} bytes", "{0} KB",
"{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB"
};
int i = 0;
while (i < format.Length && s >= 1024)
{
s = (long) (100*s/1024)/100.0;
i++;
}
return string.Format(format[i], s);
}
public string ToFuzzyByteString(long bytes)
{
double s = bytes;
string[] format = new string[]
{
"{0} bytes", "{0} KB",
"{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB"
};
int i = 0;
while (i < format.Length && s >= 1024)
{
s = (long) (100*s/1024)/100.0;
i++;
}
return string.Format(format[i], s);
}
Alte URL:
/snippet/byte-groessenangaben-als-string-formatieren-kb-mb-gb/1304