Convert Byte to MB or GB

anthonytr used Ask the Experts™

I have an application which outputs file data into a database as each file is created.  The data which is added to the database is the file size in (Bytes).  I run regular queries and report which add these fields up and give me a total size of all the documents which have been created.  Obviously, it just adds up the BYTE field so that I end up with a huge figure.  How can I then convert this figure into either MB or GB.  I understand that:

for MB it is bytes/(1024*1024)
for GB is it bytes/(1024*1024*1024)

I'm getting nowhere with this.  

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Stuart DrydenAutomation solutions architect & senior technical support engineer

divide by 1024 for each step.

be careful about 'bytes on disk' compared to file size (in the properties of the file)
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Correction (divide and not multiply):
  • for MB it is bytes/1024/1024
  • for GB is it bytes/1024/1024/1024
Stuart DrydenAutomation solutions architect & senior technical support engineer

I am thinking that divide by 1024 might be too obvious and so am wondering where other confusion might lay.

Are you taking the file size of the database container file and wondering why this does not match up to the size of the contents?

are you using some other software that could be possibly be counting up the space in used allocation units?

Where exactly are you reading the size values from and how are they described ?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mike EghtebasDatabase and Application Developer

- 1 Bit = Binary Digit;

- 8 Bits = 1 Byte;

- 1000 Bytes = 1 Kilobyte;

- 1000 Kilobytes = 1 Megabyte;

- 1000 Megabytes = 1 Gigabyte;

- 1000 Gigabytes = 1 Terabyte;

- 1000 Terabytes = 1 Petabyte;

- 1000 Petabytes = 1 Exabyte;

- 1000 Exabytes = 1 Zettabyte;

- 1000 Zettabyte = 1 Yottabyte;

- 1000 Yottabyte = 1 Brontobyte.

Open in new window



Our Kodak software outputs a batch file with file info on.  One of the fields is document size (in Bytes).
Stuart DrydenAutomation solutions architect & senior technical support engineer

You will have to run a few different tests, when you get the matching answer you will know you are taking the right values.

Sometimes the software that reports KB, MB and GB can play some nasty tricks on you.  as an example think of decimal stones and pounds.  What is the difference between 10 Stone 5 pounds and (on a display might look like 10, 5) and 10.5 stone.  A stone has 14 pounds and so 10.5 decimal stones is actually 10 stone 7 pounds.   If software reports 10.5 K this might or might not be (10 x 1024) + (0.5 x 1024)  you never know how it is reporting the numbers and you should ask yourself if you trust it.

Run calculations in different ways until you figure out how it is working.

At its most simple i would imagine the following.

Kodak picture is 123,456 bytes
when reported in Kb  this 'could be' 123000 divided by 1024 which gives you the whole KiloBytes plus 456 bytes extra. or it could be the accurate 123,456 divided by 1024.  test which is the correct answer and try to come up with some of your own attempts
divide by another 1024 gives you the MegaBytes
Divide by another 1024 gives you the GigaBytes

Software always has its own unique perspective on reporting bytes - the trick is to quantify what it is doing by testing it mathematically.
Top Expert 2014
I did this in the Immediate window.  The first step is to create an array of suffixes.
Then I calculate the base 10 log of the number and do an integer divide by three.  This gives me a value I can raise 1000 to the power of as well as index into the suffixes array.  I only went up to TB, but the suffixes can be extended by adding to the array content.
x=123:sz=log(x)/log(10):?x,format(x,"#,000"),sz,sz\3,x/(1000^(sz\3)) & " " & suffix(sz\3)
 123        	123             	2.0899051114394 	0	123 
 1234       	1,234           	3.09131515969722	1	1.234 KB
 12345      	12,345          	4.09149109426795	1	12.345 KB
 123456     	123,456         	5.09151220162777	1	123.456 KB
 1234567    	1,234,567       	6.09151466408626	2	1.234567 MB
 12345678   	12,345,678      	7.0915149455092 	2	12.345678 MB
 123456789  	123,456,789     	8.09151497716927	2	123.456789 MB
 1234567890 	1,234,567,890   	9.09151497716927	3	1.23456789 GB
 12345678901	12,345,678,901  	10.0915149772044	3	12.345678901 GB
 123456789012	123,456,789,012 	11.0915149772115	3	123.456789012 GB
 1234567890123	1,234,567,890,123	12.0915149772125	4	1.234567890123 TB

Open in new window

Depending if you count via 1000x1000 or 1024x1024 look here for definitions
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015
kilobytes are ALWAYS 1024 bytes digitally --  and so on. Using 1000 only provides an estimate, which gets farther off as the magnitude of bytes is increased since binary is 2, 4, 8, 16, 32, 64, 128, 256, 1024, ...

I liked Mike's enumeration -- didn't know the words past tera .... but in the digital world, value is divided by 1024 not 1000 as in the real world

Stuart brought up a good point about division and, actually the size taken is what it needs + the extra to fill that sector on the storage device (ie, disk, disc, stick).

aikimark (Mark)  has the beginnings of the logic for deciding how to modify the format code

imo, a good way to report the information is to send the byte value to a function to decide the magnitude of the value and send back a string for reporting. Here is a general function you can send bytes to and use for reporting:
Public Function Get_ConvertBytes(vBytes As Variant) As String
'170328 s4p
' this only goes as far as terabytes (tb)

   'initialize return value
   Get_ConvertBytes = ""
   If IsNull(vBytes) Then Exit Function
   Select Case vBytes
   Case Is > 1024 ^ 4 'terabytes
      Get_ConvertBytes = Format((vBytes / 1024 ^ 4), "#,##0.#\ \t\b")
   Case Is > 1024 ^ 3 'gigabytes
      Get_ConvertBytes = Format((vBytes / 1024 ^ 3), "#,##0.#\ \g\b")
   Case Is > 1024 ^ 2 'megabytes
      Get_ConvertBytes = Format((vBytes / 1024 ^ 2), "#,##0.#\ \m\b")
   Case Is > 1024 ^ 1 'kilobytes
      Get_ConvertBytes = Format((vBytes / 1024 ^ 1), "#,##0.#\ \k\b")
   Case Else
      Get_ConvertBytes = Format(vBytes, "#,##0\ \b\y\t\e\s")
   End Select
End Function

Open in new window

Top Expert 2014

The Log() function makes the code much simpler.  I used 1000 for illustration purposes.  You can use 1024.  It wasn't clear from the comments whether the OP needed the output in Kilo or Kibi units.
Top Expert 2014
Code packaged into a function with optional base.  You can change the 1024 base (Kibi) to 1000 (Kilo)
Function Q_29011963(ByVal parmBytes, Optional parmBase As Long = 1024) As String
    Dim vSuffix As Variant
    Dim vSize As Variant
    vSuffix = Array("", "KB", "MB", "GB", "TB")
    vSize = Int(Log(parmBytes) / Log(parmBase))
    Q_29011963 = Trim(parmBytes / (parmBase ^ (vSize)) & " " & vSuffix(vSize))
End Function

Open in new window

Some invocation examples from the Immediate window:
x=12: ?x, Q_29011963(x)
 12           12
 123          123
 1234         1.205078125 KB
 12345        12.0556640625 KB
 123456       120.5625 KB
 1234567      1.17737483978271 MB
 12345678     11.7737560272217 MB
 123456789    117.737568855286 MB
 1234567890   1.1497809458524 GB
 12345678901  11.4978094594553 GB
 123456789012               114.978094596416 GB
 1234567890123              1.12283295504585 TB

Open in new window

Who is the clown who says kilobytes are always 1024?

That's why we now have an official definition,
Endorsed as IEC/SI units
1000 = Kilobytes, 1024 = Kibi
Mega & Mebi
Etc etc
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

anthonytr, a feedback will be appreciated.
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

I do not agree with the points split suggestion. IMHO isn't fair to have one Expert getting half of the points  and 6 others splitting between them the other half of points.
Stuart DrydenAutomation solutions architect & senior technical support engineer

I strongly disagree too with the point allocation.  Firstly there were only a couple of experts that the Antony (the user who asked the question) interacted with.  Gerald not being one of them. Secondly Gerald did not interact with the originator, his first comment was a repeat and his second was calling another expert a clown and being pedantic about SI unit expression.  IMHO Gerard should be awarded ZERO.

My comments were helpful, were the first to reply, interacted with the user, gave the user understanding of the problem, had foresight into what the users actual problem was, gave a methodology the user to investigate further, my comments were referenced by other experts as good information.

I would suggest 250 / 250 points given to both myself and Vitor.  We both were gave the only answers that interacted with the user while they were still involved in the question.  All other answers were either a repeat of what we said or advanced information far beyond the users capability of understanding.

My specific objection as to the allocation reasoning given by Gerald is that the user was not helped by knowing that KiloBytes were KiBi - they did not need to know that, the user wanted help on the information they had provided.
Top Expert 2014

I would recommend that the points be split between aikimark and crystal, since we their comments were the only two with solution code to the problem.
Stuart DrydenAutomation solutions architect & senior technical support engineer

Code?  the user was not helped by code nor would they have been able to use it to understand their problem.

The main problem this website experiences is experts trying to outbid each other with more and more harder complex technical information, each more complex than the next.  This does not help a person wanting assistance to solve their problem.

I am a technical support engineer, despite being capable of giving far more complex information on a subject I pitch the level of my response to assist a person in reaching their technical goals.

This website is called Experts Exchange, i believe because it is information exchanged between experts and those requiring help.  The website is not called "Battle of the Experts", where he who can give more complex information wins.

Please in future pitch your answer at the level of the person asking the question - this website is not 'just' a knowledge base - it should be aimed at helping people.  If you want to add more high level information on a subject than is required can i suggest that you make an edit to a Wiki ?
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

re: Kilobyte vs Kibi.
Kibi is a fairly new term.  In the beginning of PCs, more than 30 years ago when I began using them, a kilobyte was 1024 bytes, in relationship to computers. Once you think you know a term, one does not tend to look it up again, so I learned something too.  The original post, however, specified 1024.

I agree that we are here to help each other.  Everyone should be spoken to cordially. Mud-slinging doesn't help anyone.
Stuart DrydenAutomation solutions architect & senior technical support engineer

Agreed in regards to the KB / Kibi issue.  It was one of the more ridiculous standard ratifications i did not agree with when it came out - but who are we to disagree with an expert panel.

They ignored the fact that we represented bits with a lowercase b, bytes with an uppercase B.

it was easy to understand bps and Bps, Kbps and KBps.  the wider IT community evolved to this understanding naturally.  Computer techies understood this meant powers of 1024.  I guess the expert panel just wanted to guard the true meaning of Kilo / Mega etc etc.

I understand the reasons behind it even though it was unwelcome.  I guess they must be quite upset that i have only ever seen a couple of examples EVER of people using the correct notation for the units.
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Rob, I still think that Stuart and I fixed the author's formula in our first comments. There's no doubts that next comments from other Experts added some value but the exact issue is that the author had the wrong formula.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial