public const string DECIMAL_FORMAT = "0.##"; public static string ToFormattedSize(int size) { float KB = 1024f; float MB = 1024f * KB; float GB = 1024f * MB; if (!size.HasValue) return "-"; else if ((float)size.Value > GB) return ((float)size.Value / GB).ToString(DECIMAL_FORMAT) + " GB"; else if ((float)size.Value > MB) return ((float)size.Value / MB).ToString(DECIMAL_FORMAT) + " MB"; else if ((float)size.Value > KB) return ((float)size.Value / KB).ToString(DECIMAL_FORMAT) + " KB"; else return ((float)size).Value.ToString(DECIMAL_FORMAT) + " B"; }
26 May 2011
C# - Format a file size
Here's how you can format a file size, specified in bytes, into a nice string for display:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment