Created
October 2, 2015 14:40
-
-
Save jbnv/130e014cfa249d71dff3 to your computer and use it in GitHub Desktop.
Simple C# class for converting an integer into a file size with suffix.
This file contains hidden or 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
| public class FileSize | |
| { | |
| long _value; | |
| long _reduced; | |
| string _units; | |
| public long Reduced { get { return _reduced; } } | |
| public string Units { get { return _units; } } | |
| public FileSize(long value) | |
| { | |
| _value = value; | |
| var units = new string[] { "B", "KB", "MB", "GB" }; | |
| var sizeUnitIndex = 0; | |
| _reduced = value; | |
| while (_reduced > 1024) | |
| { | |
| _reduced /= 1024; | |
| sizeUnitIndex++; | |
| } | |
| _units = units[sizeUnitIndex]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment