advertisement

Find Code  Advanced Search   

Free Newsletters:   
browse free vb code
Submit Code
ASP,  HTML, and XML
Database
Dates  and Math
Files  and Directories
Forms  and Controls
Lists,  Collections, and Arrays
Miscellaneous
Multimedia/Games
Office/VBA
Network/Internet
Registry
Screen/Graphics
String  Manipulation
System/API
Windows  2000/XP
VB.NET/ASP.NET



advertisement
Convert Bytes to the Appropriate Format (Bytes, KB, MB, GB)

Author: Anonymous
Category: Dates and Math
Type: Snippets
Difficulty: Beginning

Version Compatibility:  Visual Basic 5   Visual Basic 6

More information: Convert bytes to the best format (Bytes, KB, MB, GB)

Examples:

  • SetBytes(74) 'returns "74 Bytes"
  • SetBytes(5037) 'returns "4.92 KB"
  • SetBytes(6383838) 'returns "6.09 MB"
  • SetBytes(3368383278) 'returns "3.14 GB"

This code has been viewed 336167 times.

Instructions: Copy the declarations and code below and paste directly into your VB project.


Declarations:

'none

Code:

Function SetBytes(Bytes) As String

On Error GoTo hell

If Bytes >= 1073741824 Then
    SetBytes = Format(Bytes / 1024 / 1024 / 1024, "#0.00") _
         & " GB"
ElseIf Bytes >= 1048576 Then
    SetBytes = Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
    SetBytes = Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes < 1024 Then
    SetBytes = Fix(Bytes) & " Bytes"
End If

Exit Function
hell:
SetBytes = "0 Bytes"
End Function

Text Boxes

Sponsored Links