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

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

Guy Fawkes(Posted 2009) [#1]
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:



~DarkShadowWing~


jfk EO-11110(Posted 2009) [#2]
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.


_PJ_(Posted 2009) [#3]
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



bytecode77(Posted 2009) [#4]
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!


jfk EO-11110(Posted 2009) [#5]
Don't forget the Gigabyte and the Terabyte - and I'm pretty sure they already got one more of them - Weirdobyte or something.


xlsior(Posted 2009) [#6]
Giga -> Tera -> Peta -> Exa


Gabriel(Posted 2009) [#7]
You do realize that filesize has no bearing on the duration of a song in most formats, don't you?


Guy Fawkes(Posted 2009) [#8]
yes


Yukio(Posted 2009) [#9]
I believe that maybe the topic creator could be interested into a discussion about kilobytes and kibibytes:
Data rate units


Ked(Posted 2009) [#10]
Exa -> Zetta -> Yotta

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

Ouch.


Adam Novagen(Posted 2009) [#11]
Giga -> Tera -> Peta -> Exa

Geez, you'd think that, logically, they'd go on with something like Tera -> Quadra -> Quinta etc., but NOOO...


Guy Fawkes(Posted 2009) [#12]
LOL!