How to return KB, MB, Bytes.. etc..

Blitz3D Forums/Blitz3D Programming/How to return KB, MB, Bytes.. etc..

I created my own function to display KB, MB, or Bytes. but im not sure what im doing wrong here.

You dont have to help if you dont want to, but i'd appreciate if you did =)

Code:

Graphics3D 800, 600, 32, 2


size# = FileSize("c:/windows/system32/calc.exe")

While Not KeyDown(1)

UpdateWorld
RenderWorld

Text 10, 10, "size: "+size#

Text 10, 30, "size type: "+size#+" "+getsize$(size#)

Flip
Wend

Function getsize$(size#)

If size# >= size#*1000

stype$ = "MB"

Else If size# < size#*1000

stype$ = "KB"

EndIf

Return stype$

End Function


~DarkShadowWing~

If size# >= size#*1000 ??

If 100 >= 100 * 1000 well I think this will never happen.

It's a bit confusing since you need 2 things: the B/KB/MB thing, as well as how many of it. Maybe for simplicity you best store both in one string:
function getsize$(size)
if size >=1048576 then 
  return ""+(size/1048576)+" MB"
endif
if size >=1024 then
  return ""+(size/1024)+" KB"
endif
return ""+(size)+" B"
end function


BTW a kilobyte has 1024 bytes, because with the bits required to count up to one thousand you can also count up to 1023 - this standard was set for economical reasons, based on binary adressing.
If the long floating point value disturbes then you may also truncate the result, sample given:
if size >=1048576 then 
  size2$=""+(size/1048576)+"     "
  size2$=left$(size2$,instr(size2$,".")+2)
  size2$=replace$(size2$," ","")
  return size2$+" MB"
endif

This should make 3.12 out of 3.1223432423

Since it's using strings it's not very fast tho.

This gives the approximate value to the nearest kB, MB, GB etc. discarding decimals, but can be easily added to incorporate greater than Terabytes if needed.
Function Bytesize$(size)
	Appel$="B"
	For Iterations%=0 To 9
		If size%<1024
			Exit
		End If
		size=Floor#(size/1024)
	Next
	Select Iterations%
		Case 1: Appel$="kB":SubAppel$="B"
		Case 2: Appel$="MB":SubAppel$="kB"
		Case 3: Appel$="GB":SubAppel$="MB"
		Case 4: Appel$="TB":SubAppel$="GB"
	End Select
	Return size+" "+Appel$
End Function


Function SizeName(size)
Unit$ = "Bytes"
If Size > 1000 Then
	Size = Size / 1024
	Unit$ = "KB"
	If Size > 1000 Then
		Size = Size / 1024
		Unit$ = "MB"
	EndIf
EndIf
Return Size + " " + Unit
End Function


be aware of the difference of 1000 and 1024!

Don't forget the Gigabyte and the Terabyte - and I'm pretty sure they already got one more of them - Weirdobyte or something.

Giga -> Tera -> Peta -> Exa

You do realize that filesize has no bearing on the duration of a song in most formats, don't you?

yes