What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleFormat a BIG number of bytes in KB, MB, GB, TB, etc. by using an array of postfixes in VB .NET
DescriptionThis example shows how to format a BIG number of bytes in KB, MB, GB, TB, etc. by using an array of postfixes in VB .NET.
Keywordsformat, bytes, KB, MB, GB, VB.NET
CategoriesAPI, Files and Directories
Thanks to Dougal Morrison for the original VB 6 example.

Unfortunately the StrFormatByteSize API function (click here for an example) uses long integers to store byte values so it can only handle up to 2,147,483,647 bytes or a bit less than 2 GB. Modern computers often work with values greater than 2 GB and in some cases more than 1 TB (1 terabyte = 1024 gigabytes).

The Numeric2Bytes function makes an array of the postfixes (MB, GB, etc.) for different powers of 1024 and then finds the one that is appropriate. It formats the value with two digits to the right of the decimal place, using the correct

' Returns string with binary notation of b bytes,
' rounded to 2 decimal places , eg
' 123="123 Bytes", 2345="2.29 KB",
' 1234567="1.18 MB", etc
' b : double : numeric to convert
Function Numeric2Bytes(ByVal b As Double) As String
    Dim bSize(8) As String
    Dim i As Integer

    bSize(0) = "Bytes"
    bSize(1) = "KB" 'Kilobytes
    bSize(2) = "MB" 'Megabytes
    bSize(3) = "GB" 'Gigabytes
    bSize(4) = "TB" 'Terabytes
    bSize(5) = "PB" 'Petabytes
    bSize(6) = "EB" 'Exabytes
    bSize(7) = "ZB" 'Zettabytes
    bSize(8) = "YB" 'Yottabytes

    b = CDbl(b) ' Make sure var is a Double (not just
        ' variant)
    For i = UBound(bSize) To 0 Step -1
        If b >= (1024 ^ i) Then
            Numeric2Bytes = ThreeNonZeroDigits(b / (1024 ^ _
                i)) & " " & bSize(i)
            Exit For
        End If
End Function

' Return the value formatted to include at most three
' non-zero digits and at most two digits after the
' decimal point. Examples:
'         1
'       123
'        12.3
'         1.23
'         0.12
Private Function ThreeNonZeroDigits(ByVal value As Double) _
    As String
    If value >= 100 Then
        ' No digits after the decimal.
        Return Format$(CInt(value))
    ElseIf value >= 10 Then
        ' One digit after the decimal.
        Return Format$(value, "0.0")
        ' Two digits after the decimal.
        Return Format$(value, "0.00")
    End If
End Function
Note that this program does not produce results that match the StrFormatByteSize API function. First, this version never uses more than 3 non-zero digits. If displays 1000 as 0.98 KB while StrFormatByteSize displays it as 1000 bytes.

Second, this program rounds values while StrFormatByteSize seems to truncate them somehow. For example, 1023999 is roughly 999.999 KB. StrFormatByteSize formats this value as 999 KB (999 KB is actually 1022976, a difference of 1023) while this program formats the value as 0.98 MB.

If you really want this program to match the StrFormatByteSize results exactly, you'll have to modify the code.

See also:

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.