Created
July 31, 2019 12:43
-
-
Save ramonsmits/d4f0f97b188ef615c0edf57eb714cccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net | |
public static class SizeFormatter | |
{ | |
static String StrFormatByteSize (long length, IFormatProvider provider) | |
{ | |
const int kb = 1024; | |
string[] suffix = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; | |
if (length == 0) return "0" + suffix[0]; | |
var bytes = Math.Abs(length); | |
var index = (int)Math.Floor(Math.Log(bytes, kb)); | |
var num = bytes / Math.Pow(kb, index); | |
return (Math.Sign(length) * num).ToString("N", provider) + suffix[index]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment