derive kilobytes from bytes

L

Larry

Is there a formula to convert bytes to kilobytes, which would work with
any number of bytes?

I want this for a macro in which I save a backup copy of a template, and
at the end of the macro I display a msgbox with some information
including the size of the newly saved template. I want this to be in
the same format that you see in the File Properties dialog, with both
the bytes and the kilobytes. The problem is that this involves more
than simply dividing the number of bytes by 1.024 to get the number of
kilobytes. That computation works with smaller files, but not with
larger files.

Thanks,
Larry
 
L

Larry

I'm sorry for my primitive knowledge of math, but it seems that if the
number of bytes in a file is over 1,000,000 I can get close to the
correct result in MBs by dividing by 1.024 and then dividing again by
1.024. Alternatively, I get an approximately close result to that by
simply dividing by 1.024x1.024, or 1.04858.

Thus, in the Properties page of a particular template document, the
number of bytes is 1,451,008 bytes and the number of megabtyes is 1.38
MB. If I divide 1,451,008 by 1.024 twice, I get 1,383,789.00 or 1.38
MB, the same as what the Properties page shows. If I divide 1,451,008
by 1.04858 I get 1,383,784.00, which though different is still rounded
off as 1.38 MB.

The question is, what is the formula that Word itself uses?

Larry
 
L

Larry

Also, does VBA give the ability to round off a number, so that, for
example, a number like
1,386,718 (derived from dividing the number of bytes by 1.024 twice)
becomes 1.39MB, following the format used in in the File Properties
page?
 
L

Larry

Cool. Word does the averaging automatically.

Thus to get the number 1,348,034 into a format #.## MB I do this:

bts = 1348034
sbts = bts / 1000000
MsgBox Format(sbts, "###.##")

the result is 1.35.
 
B

Bob Buckland ?:-\)

Hi Larry,

The properties dialog is populated with data from
Windows. 1,024 bytes is 1KB.

==========I'm sorry for my primitive knowledge of math, but it seems that if the
number of bytes in a file is over 1,000,000 I can get close to the
correct result in MBs by dividing by 1.024 and then dividing again by
1.024. Alternatively, I get an approximately close result to that by
simply dividing by 1.024x1.024, or 1.04858.

Thus, in the Properties page of a particular template document, the
number of bytes is 1,451,008 bytes and the number of megabtyes is 1.38
MB. If I divide 1,451,008 by 1.024 twice, I get 1,383,789.00 or 1.38
MB, the same as what the Properties page shows. If I divide 1,451,008
by 1.04858 I get 1,383,784.00, which though different is still rounded
off as 1.38 MB.

The question is, what is the formula that Word itself uses?

Larry >>
--
I hope this helps you,

Bob Buckland ?:)
MS Office System Products MVP

*Courtesy is not expensive and can pay big dividends*

The Office 2003 System parts explained
http://microsoft.com/uk/office/preview/system.asp

MS on 'Why Office System 2003'
http://microsoft.com/mscorp/execmail/2003/10-13productivity.asp
 
L

Larry

I know that, Bob. My question concerns not deriving kilobytes but
deriving megabytes. How many bytes is a megabyte? Is it 1,048,576.00?

Larry
 
T

TF

Larry

Basically the difference and confusion is caused by the computer geeks
deciding (many moons ago) to use the term Kilo and Mega (etc) to represent
2^10 and 2^20 and so on - though they did have good reason.

So this equates to: 1024, 1048576, 1073741824, 1099511627776 for Kilo,
Mega, Giga and Tera. Unfortunately, manufacturers of hard disks decided to
hoodwink the unsuspecting layman by using the non-computer measurements for
devices that are fitted into computers. This is why a HDD is much smaller
than you think: you lose a significant percentage to formatting and only see
the value displayed in computerese!

Terry Farrell



I know that, Bob. My question concerns not deriving kilobytes but
deriving megabytes. How many bytes is a megabyte? Is it 1,048,576.00?

Larry
 
L

Larry

This macro works perfectly, except that in some instances the file size
it returns in MBs is one hundredth of an MB higher than that shown in
Explorer and in the document's Proprerties dialog box.

So, for example, the size of my Normal.dot as shown in the Properties
page is currently:

1,298,944 bytes
1.23MB

But when I run the below code (which appears at the end of a macro in
which I've made two backup copies of Normal), I get 1.24 MB, not 1.23MB.
When there is a mistake, it's always the same: one number higher in the
hundredths column. How can I get the exact same result in my macro as
that provided by Windows and Word?

bts = ActiveDocument.BuiltInDocumentProperties("Number Of Bytes")
myBts = Format(bts, "###,###,###")
Kilobts = myBts / 1024
myKilobts = Format(Kilobts, "###,###.0")

MgBts = myKilobts / 1024
myMgBts = Format(MgBts, "###,###.00")

ActiveDocument.Close

If myKilobts < 1000 Then
MsgBox myDatedTemplate & " saved in Templates folder and My System
folder." & vbCr & _
"Size: " & myKilobts & " KB (" & myBts & " bytes)", vbOKOnly, "Normal
Backup"
Else
MsgBox myDatedTemplateName & " saved in Templates folder and My System
folder." & vbCr & _
"Size: " & myMgBts & " MB (" & myBts & " bytes)", vbOKOnly, "Normal
Backup"
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top