Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to convert bytes to KB or MB or GB or TB or PB in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Function convert_bytes(ByVal bytes As Long) As String
		Dim sizes As String() = {"B", "KB", "MB", "GB", "TB", "PB"}
        Dim dbytes As Double = bytes
        Dim i As Integer
		
		Do While (i < sizes.Length AND bytes >= 1024) 
			 dbytes = bytes / 1024.0
			 i += 1		
			 bytes /= 1024
		Loop
        Return String.Format("{0:0.##} {1}", dbytes, sizes(i))
    End Function

	Public Shared Sub Main()
        Console.WriteLine(convert_bytes(554432))
        Console.WriteLine(convert_bytes(3554432))
        Console.WriteLine(convert_bytes(33554432))
        Console.WriteLine(convert_bytes(333554432))
        Console.WriteLine(convert_bytes(3333554432))
        Console.WriteLine(convert_bytes(33333554432))
        Console.WriteLine(convert_bytes(333333554432))
        Console.WriteLine(convert_bytes(3333333554432))
        Console.WriteLine(convert_bytes(33333333554432))
        Console.WriteLine(convert_bytes(333333333554432))
        Console.WriteLine(convert_bytes(3333333333554432))
    End Sub
End Class





' run:
'
' 541.44 KB
' 3.39 MB
' 32 MB
' 318.1 MB
' 3.1 GB
' 31.04 GB
' 310.44 GB
' 3.03 TB
' 30.32 TB
' 303.17 TB
' 2.96 PB
'

 



answered Nov 5, 2021 by avibootz
edited Nov 5, 2021 by avibootz
...