On Rabu, 30 Juni 2010 0 komentar

Bagaimana ya caranya memformat size memori lewat VB.NET ?, nah, disini saya ingin berbagi kodenya nih...silahkan di coba...





Kode:

Enum FormatMemorySizeUnits
BestGuess
Bytes
Kilobytes
Megabytes
Gigabytes
End Enum


' Mengconvert sebuah angka dari bytes ke dalam 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
' pengecekan error sederhana
If value < 0 Then Throw New ArgumentException("Value can't be negative")

' Mendapatkan unit terbaik, jika di minta
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

' evaluasi nilai desimal
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

' Mendapatkan string perwakilan
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


Semoga bisa memberi pengetahuan baru buat teman-teman.... ^_^

0 komentar:

Posting Komentar