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.
Description
Keywordsformat, bytes, KB, MB, GB
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.
        FormatBytes = Format$(num_bytes, "0") & " bytes"
    ElseIf num_bytes <= ONE_KB * 999 Then
        ' Format in KB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_KB) & " KB"
    ElseIf num_bytes <= ONE_MB * 999 Then
        ' Format in MB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_MB) & " MB"
    ElseIf num_bytes <= ONE_GB * 999 Then
        ' Format in GB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_GB) & " GB"
    ElseIf num_bytes <= ONE_TB * 999 Then
        ' Format in TB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_TB) & " TB"
    ElseIf num_bytes <= ONE_PB * 999 Then
        ' Format in PB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_PB) & " PB"
    ElseIf num_bytes <= ONE_EB * 999 Then
        ' Format in EB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_EB) & " EB"
    ElseIf num_bytes <= ONE_ZB * 999 Then
        ' Format in ZB.
        FormatBytes = ThreeNonZeroDigits(num_bytes / _
            ONE_ZB) & " ZB"
    Else
        ' Format in YB.
        FormatBytes = 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.
        ThreeNonZeroDigits = Format$(CInt(value))
    ElseIf value >= 10 Then
        ' One digit after the decimal.
        ThreeNonZeroDigits = Format$(value, "0.0")
    Else
        ' Two digits after the decimal.
        ThreeNonZeroDigits = 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