'Variant one (original)
'call with MakeNiceSize(YourSizeInBytes, "0")
Public suffix() As String = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
Function MakeNiceSize(ByVal size As Double, ByVal run As Integer)
Dim ReturnedString As String = ""
If size >= 1024 Then
ReturnedString = MakeNiceSize(size / 1024, run + 1)
Else
ReturnedString = Math.Round(size, 2).ToString("00.00") & " " & suffix(run)
End If
Return ReturnedString
End Function
'Variante 2 (using loop)
'call with MakeNiceSize(YourSizeInBytes)
Function MakeNiceSize(ByVal size As Double)
Dim suffix() As String = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
Dim run As Integer = 0
While size >= 1024
size /= 1024
run += 1
End While
Return Math.Round(size, 2).ToString("00.00") & " " & suffix(run)
End Function
'variante 3 (possibility to "choose" size)
'modes are: "auto" (chooses best),"B","KB","MB","GB","TB","PB","EB"
'for auto simple use MakeNiceSize(YourSizeInBytes)
'for MB simple use MakeNiceSize(YourSizeInBytes,"MB")
'returns false if any error occured
Function MakeNiceSize(ByVal size As Double, optional ByVal mode As String = "auto")
Dim suffix() As String = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
Dim run As Integer = 0
If mode = "auto" Then
While size >= 1024
size = size / 1024
run = run + 1
End While
Return Math.Round(size, 2).ToString("00.00") & " " & suffix(run)
ElseIf mode <> "auto" Then
If suffix.Contains(mode) Then
While suffix(run) <> mode
size = size / 1024
run = run + 1
End While
Else
Return False
End If
Return Math.Round(size, 2).ToString("00.00") & " " & suffix(run)
End If
Return False
End Function