devxlogo

FormatMemorySize – Format a value in bytes

FormatMemorySize – Format a value in bytes

Enum FormatMemorySizeUnits    BestGuess    Bytes    Kilobytes    Megabytes    GigabytesEnd Enum' convert a number of bytes into Kbytes, Megabytes, or GigabytesFunction FormatMemorySize(ByVal value As Long, _    ByVal unit As FormatMemorySizeUnits, Optional ByVal decimalDigits As _    Integer = 2, Optional ByVal omitThousandSeps As Boolean = False) As String    ' simple error checking     If value ' get the best unit, if required    If unit = FormatMemorySizeUnits.BestGuess Then        Select Case value            Case Is ' evaluate the decimal value    Dim val As Decimal    Dim suffix As String     Select Case unit        Case FormatMemorySizeUnits.Bytes            val = value        Case FormatMemorySizeUnits.Kilobytes            val = value / 1024            suffix = "K"        Case FormatMemorySizeUnits.Megabytes            val = value / 1048576            suffix = "M"        Case FormatMemorySizeUnits.Gigabytes            val = value / 1073741824            suffix = "G"    End Select    ' get the string representation    Dim format As String    If omitThousandSeps Then        format = "F" & decimalDigits.ToString    Else        format = "N" & decimalDigits.ToString    End If    Return val.ToString(format) & suffixEnd Function

devx-admin

Share the Post: