Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

FormatMemorySize - Format a value in bytes

Total Hit ( 2767)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
Enum FormatMemorySizeUnits
  BestGuess
  Bytes
  Kilobytes
  Megabytes
  Gigabytes
End Enum


' convert a number of bytes into Kbytes, Megabytes, or Gigabytes

Function 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 < 0 Then Throw New ArgumentException("Value can't be negative")

  ' get the best unit, if required
  If unit = FormatMemorySizeUnits.BestGuess Then
    Select Case value
      Case Is < 1023
        unit = FormatMemorySizeUnits.Bytes
        decimalDigits = 0
      Case Is < 1024 * 1023
        unit = FormatMemorySizeUnits.Kilobytes
      Case Is < 1048576 * 1023
        unit = FormatMemorySizeUnits.Megabytes
      Case Else
        unit = FormatMemorySizeUnits.Gigabytes
    End Select
  End If

  ' 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) & suffix
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.