I am using the bellow code to convert the kb to mb,gb,etc.
Can any expert tell me if the code is ok?
Because the data i get from Shell is Kilobytes,i am using the “byteCount = byteCount * 1024”.
If remove the line of “byteCount = byteCount * 1024” then the results is wrong,like if i have a file 1GB then after conversion show up as 1MB,etc.
Dim unit, units(-1) As String
units = Array("Bytes","K", "M", "G", "T")
Dim bytecount as double
Dim s,i As Integer
for i=0 to Listbox1.ListCount-1
byteCount = byteCount + val(Listbox1.Cell(i, 4))
next
byteCount = byteCount * 1024
For s = 0 To ubound(units)
If byteCount < 1024 Then
unit = units(s)
Exit
End
byteCount = byteCount / 1000
Next
If unit = "" Then
unit = units.pop()
End
first off… use 1024 or 1000 not both (1024 is right value)
why bother to multiply by 1024 when you are just gonna divide it anyways… this forces one more * and / operation
Dim unit, units(-1) As String
units = Array("Bytes","K", "M", "G", "T")
Dim bytecount as double
Dim s,i,j As Integer
for i=0 to Listbox1.ListCount-1
byteCount = byteCount + val(Listbox1.Cell(i, 4))
next
///// byteCount = byteCount * 1024
j=0
while byteCount>1024
j=1
byteCount=byteCount/1024
wend
unit=units(j)
its 3am, and I’m a bit foggy…but look at this and see if you can make it work instead
Thank you @Dave S
Because the value i get from shell is kBytes no bytes,so my conversion fail…
lets say i get the folder documents with “Cmd+i”
7.279.978.904 bytes (7,37 GB on disk) for 39.999 items
Now from code above i want get the size from documents folder.
Bytecount 7212116, '<-- this is wrong the default must be (7385206784)
Bytecount=Bytecount /1024 ' ---> 7043,08203125
TotalSize =6.88K 'wrong actual size is 7.37GB
This is the reason i use byteCount = byteCount * 1024 to double the value and get the correct result…
actual size is 6.88 gig IS Correct assumiong your actual size is “bytes” is 7,385,206,784
7385206784 / 1024 is 7212116 KB
7212116 KB / 1024 is 7043.082 MB
7043.082 MB /1024 is 6.878 GB
remember a GIG is 1073741824 bytes NOT 1000000000 bytes
So you have 7.37 BILLION bytes, but only 6.88 Gigabytes … so it depends on what you want to display… if you want 7.37 as your answer use 1000 in all places, otherwise you use 1024 in all places. but never mix them
And oh by the way… even computer manufacturers are NOT consisitent in how they portray these numbers
To further that statement, OS X has shown 1GB as 1 billion bytes by default in Finder since 10.6. I think there’s still a hidden setting to switch it back to base-2, so that’ll just add to the confusion.