Calculating Bytes/KB/MB/Gb...

  • Thread starter Thread starter PiotrKolodziej
  • Start date Start date
P

PiotrKolodziej

Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Thanks
PK
 
PiotrKolodziej said:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?

bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB
 
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this
variable as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?
Thanks
PK

You have to do it yourself Piotr :-(

Do it once and keep it somewhere safe, it's a useful function to have!
 
There is nothing in the framework for this (that I know of), mainly
because:
1) It have a very specific use.
2) the calculation itself is trivial
3) it's troubled by a number of formatting issues (commas or spaces?
KB or Ko?)
 
I think my answer was "No".

Your answer was:
bytes / 1024 == KB
bytes / 1024^2 == MB
bytes / 1024^3 == GB
bytes / 1024^4 == TB


You can write your own FormatProvider if you wish.

I knew it and the question was due to something else.
 
Do it once and keep it somewhere safe, it's a useful function to have!

Indeed.
Anyway thanks you for your posts.
 
PiotrKolodziej said:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a fast calculation or i have to
divide by 1024, check if result is zero, if not divide again and so on...?

switch((int)(System.Math.Log(bytecount) / System.Math.Log(1024)))
{
case 0:
return @"bytes";
case 1:
return @"KB";
case 2:
return @"MB";
case 3:
return @"GB";
case 4:
return @"TB";
default:
reutrn @"REALLY HUGE!";
}
 
(int)(System.Math.Log(bytecount) / System.Math.Log(1024))

Although log will work (rounding permitting), it seems overkill here...
personally I'd just keep ">> 10"-ing until I get a zero, and use the
previous value for the number, keeping track of the number of shifts
for the kb, Mb, Gb, Tb etc... It should be a lot less FLOPs... (i.e.
none)

Marc
 
How about something along the lines of this:

long number = 100;
string formattedNumber;

if (number > 1073741824)
formattedNumber = String.Format("{0} Gb", number /
1073741824);
else if (number > 1048576)
formattedNumber = String.Format("{0} Mb", number / 1048576);
else if (number > 1024)
formattedNumber = String.Format("{0} Kb", number / 1024);
else
formattedNumber = String.Format("{0} bytes", number);

MessageBox.Show(formattedNumber);

Steve
 
Marc Gravell said:
Although log will work (rounding permitting), it seems overkill here...
personally I'd just keep ">> 10"-ing until I get a zero, and use the
previous value for the number, keeping track of the number of shifts
for the kb, Mb, Gb, Tb etc... It should be a lot less FLOPs... (i.e.
none)

Log is simple and requires no recursion, which is the only reason I offered
it. Personally, I would use a bitshift myself.
 
Back
Top