Language: Add BytesToString

This commit is contained in:
Jonathan Jenne 2022-01-12 10:41:56 +01:00
parent bdae2d5ee5
commit 7d9beaceca

View File

@ -191,4 +191,15 @@ Public Class Utils
Return False
End Function
Public Shared Function BytesToString(ByteCount As Long) As String
Dim oSuffixes As New List(Of String) From {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
If ByteCount = 0 Then
Return "0" & oSuffixes.First()
End If
Dim oBytes = Math.Abs(ByteCount)
Dim oIndex = Convert.ToInt32(Math.Floor(Math.Log(oBytes, 1024)))
Dim oNum = Math.Round(oBytes / Math.Pow(1024, oIndex), 1)
Return (Math.Sign(ByteCount) * oNum).ToString() & oSuffixes.Item(oIndex)
End Function
End Class