Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFormat a BIG number of bytes in KB, MB, GB, TB, etc. in VB .NET
DescriptionThis example shows how to format a BIG number of bytes in KB, MB, GB, TB, etc. in VB .NET.
Keywordsformat, bytes, KB, MB, GB, VB.NET
CategoriesAPI, Files and Directories
 
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).

This program formats byte values in KB, MB, GB, TB, petabytes (1 PB = 1024 TBs), EB (1 exabyte = 1024 PBs), ZB (1 zettabyte = 1024 EBs), and YB (1 yottabyte - 1024 ZBs). It generates the formatted result directly rather than using the API function.

The FormatBytes function determines the value's order of magnitude. It divides the value by that order and calls function ThreeNonZeroDigits to format the result. For example, if num_bytes is greater than 999 * 1 KB and less than or equal to 999 * 1 MB, then the function formats the result in MB.

Function ThreeNonZeroDigits formats a value with at most three non-zero digits (at least if the value <= 999) and at most two digits after the decimal point.

 
' Return a formatted string representing
' the number of bytes.
Private Function FormatBytes(ByVal num_bytes As Double) As _
    String
    Const ONE_KB As Double = 1024
    Const ONE_MB As Double = ONE_KB * 1024
    Const ONE_GB As Double = ONE_MB * 1024
    Const ONE_TB As Double = ONE_GB * 1024
    Const ONE_PB As Double = ONE_TB * 1024
    Const ONE_EB As Double = ONE_PB * 1024
    Const ONE_ZB As Double = ONE_EB * 1024
    Const ONE_YB As Double = ONE_ZB * 1024
    Dim value As Double
    Dim txt As String

    ' See how big the value is.
    If num_bytes <= 999 Then
        ' Format in bytes.
        Return Format$(num_bytes, "0") & " bytes"
    ElseIf num_bytes <= ONE_KB * 999 Then
        ' Format in KB.
        Return ThreeNonZeroDigits(num_bytes / ONE_KB) & " " & _
            "KB"
    ElseIf num_bytes <= ONE_MB * 999 Then
        ' Format in MB.
        Return ThreeNonZeroDigits(num_bytes / ONE_MB) & " " & _
            "MB"
    ElseIf num_bytes <= ONE_GB * 999 Then
        ' Format in GB.
        Return ThreeNonZeroDigits(num_bytes / ONE_GB) & " " & _
            "GB"
    ElseIf num_bytes <= ONE_TB * 999 Then
        ' Format in TB.
        Return ThreeNonZeroDigits(num_bytes / ONE_TB) & " " & _
            "TB"
    ElseIf num_bytes <= ONE_PB * 999 Then
        ' Format in PB.
        Return ThreeNonZeroDigits(num_bytes / ONE_PB) & " " & _
            "PB"
    ElseIf num_bytes <= ONE_EB * 999 Then
        ' Format in EB.
        Return ThreeNonZeroDigits(num_bytes / ONE_EB) & " " & _
            "EB"
    ElseIf num_bytes <= ONE_ZB * 999 Then
        ' Format in ZB.
        Return ThreeNonZeroDigits(num_bytes / ONE_ZB) & " " & _
            "ZB"
    Else
        ' Format in YB.
        Return ThreeNonZeroDigits(num_bytes / ONE_YB) & " " & _
            "YB"
    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")
    Else
        ' 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.
  Updated