I'm working on an application that reads files from user selected folders and displays the names and file sizes in a grid. I want the file sizes to be formatted appropriate to their size. For example if a file is 50 bytes in size, it should say "50 B." If it's 200,000 bytes, it should be displayed as "195.3 KB." If it's 2,000,000 bytes, it should be displayed as "1.9 MB."
So I created the class displayed below. Instantiate it by passing in the number you want to convert and identifying what unit that number is in. Then if you know ahead of time what unit you want to convert that number to, call the relevant property. For example if you want to output in Megabytes, call the Megabytes() property. Or, as in my case, if you want it to automatically determine the best unit to output as, call the AutoUnit() property.
The FileInfo class is used to get the attributes of the file "testfile.jpg." Then the cDataUnitConverstions class is instantiated, passing in the length of the test file and identifying that length as being in Bytes. From there the Label1.Text property is set to be equal to the AutoUnit value, which would be something like "345.12 KB" depending on how big the test file really is.
Below is the class. Some next steps would include expanding it to output as strings or numeric data types, toggle on or off the unit labels in the string (like "50 MB" vs. "50"), and more. But hopefully this will get you going in the right direction and you can customize as you see fit.
Public Class cDataUnitConversions
#Region " Enums, Global Variables and Constants "
Public Enum DataUnits
Bits
Bytes
Kilobytes
Megabytes
Gigabytes
Terabytes
End Enum
Dim _inputValue As Double
Dim _inputUnit As DataUnits
Const BitsInByte As Double = 8
Const BytesInKilobyte As Double = 1024
Const BytesInMegabyte As Double = 1048576
Const BytesInGigabyte As Double = 1073741824
Const BytesInTerabyte As Double = 1099511627776
#End Region
#Region " Properties "
Public ReadOnly Property Bits() As String
Get
Return String.Format("{0:N}", _
InputAsBytes() * BitsInByte) & " b"
End Get
End Property
Public ReadOnly Property Bytes() As String
Get
Return String.Format("{0:N}", InputAsBytes()) & " B"
End Get
End Property
Public ReadOnly Property Kilobytes() As String
Get
Return String.Format("{0:N}", _
InputAsBytes() / BytesInKilobyte) & " KB"
End Get
End Property
Public ReadOnly Property Megabytes() As String
Get
Return String.Format("{0:N}", _
InputAsBytes() / BytesInMegabyte) & " MB"
End Get
End Property
Public ReadOnly Property Gigabytes() As String
Get
Return String.Format("{0:N}", _
InputAsBytes() / BytesInGigabyte) & " GB"
End Get
End Property
Public ReadOnly Property Terabytes() As String
Get
Return String.Format("{0:N}", _
InputAsBytes() / BytesInTerabyte) & " TB"
End Get
End Property
Public ReadOnly Property AutoUnit() As String
Get
Return GetAutoUnit()
End Get
End Property
#End Region
#Region " Public Methods "
Public Sub New(ByVal inputUnit As DataUnits, _
ByVal inputValue As Double)
_inputValue = inputValue
_inputUnit = inputUnit
End Sub
#End Region
#Region " Private Methods "
Private Function GetAutoUnit() As String
Dim Output As String
Dim InputBytes As Double = InputAsBytes()
Select Case InputBytes
Case Is < BytesInKilobyte
Output = String.Format("{0:N}", _
InputBytes * BitsInByte) & " B"
Case Is < BytesInMegabyte
Output = String.Format("{0:N}", _
InputBytes / BytesInKilobyte) & " KB"
Case Is < BytesInGigabyte
Output = String.Format("{0:N}", _
InputBytes / BytesInMegabyte) & " MB"
Case Is < BytesInTerabyte
Output = String.Format("{0:N}", _
InputBytes / BytesInGigabyte) & " GB"
Case Is >= BytesInTerabyte
Output = String.Format("{0:N}", _
InputBytes / BytesInTerabyte) & " TB"
Case Else
Output = _inputValue
End Select
Return Output
End Function
Private Function InputAsBytes() As Double
Dim InputBytes As Double
Select Case _inputUnit
Case DataUnits.Bits
InputBytes = _inputValue / BitsInByte
Case DataUnits.Bytes
InputBytes = _inputValue
Case DataUnits.Kilobytes
InputBytes = _inputValue * BytesInKilobyte
Case DataUnits.Megabytes
InputBytes = _inputValue * BytesInMegabyte
Case DataUnits.Gigabytes
InputBytes = _inputValue * BytesInGigabyte
Case DataUnits.Terabytes
InputBytes = _inputValue * BytesInTerabyte
Case Else
InputBytes = -1
End Select
Return InputBytes
End Function
#End Region
End Class