Questa funzione VB.net formatta il valore (double) passato come argomento nel più appropriato in Byte, KB, MB o GB (string).
Codice
Function FormattaDimensione(ByVal DimensioneInBytes As Double) As String
' Ritorna una stringa formattata che mostra il valore passato
' come argomento nell'equivalente in Bytes, KBytes, MBytes o GBytes
'
' Utilizzo: FormattaDimensione(10000) --> 9,77 KB
If DimensioneInBytes < 0 Then
Return DimensioneInBytes.ToString
ElseIf DimensioneInBytes < 1024 Then
Return String.Format("{0:N0} B", DimensioneInBytes)
ElseIf DimensioneInBytes < 1048576 Then
Return String.Format("{0:N2} KB", DimensioneInBytes / 1024)
ElseIf DimensioneInBytes < 1073741824 Then
Return String.Format("{0:N2} MB", DimensioneInBytes / 1048576)
Else
Return String.Format("{0:N2} GB", DimensioneInBytes / 1073741824)
End If
End Function
