Page view counter

Format filesize in kB or MB

Last post 07-30-2008 8:52 AM by alperdotnet. 4 replies.

Sort Posts:

  • Format filesize in kB or MB

    08-08-2003, 2:41 PM
    • Loading...
    • ShadowDanser
    • Joined on 06-17-2002, 5:09 PM
    • Posts 276
    • Points 1,380
    Hello,

    When I have read a file with x.xxx.xxx.xxx bytes it is sometimes handy to show it in an diverent format than bytes.

    Is there a handy way to do this?
    Or is there a function for it?

    When it is a big file I want to see a MB size and when it is a lower file size then I want to see a kB size for example.

    Thanks!

    Please remember to 'Mark as Answer' if this post answered your question!
  • Re: Format filesize in kB or MB

    08-08-2003, 3:09 PM
    • Loading...
    • bsmith80
    • Joined on 05-12-2003, 9:14 AM
    • Hinton, WV
    • Posts 92
    • Points 460
    There isn't anything built in that I know of but it's really easy to accomplish this. I normally use something like this:

    if (fsize < 1024)
    // code to format fsize as bytes
    else if ((fsize > 1024) && (fsize < 1048576))
    // code to format fsize as KB
    else if (fsize > 1048576)
    // code to format fsize as MB


    If there is something built in to do this, I would like to know about it too.
  • Re: Format filesize in kB or MB

    08-08-2003, 3:14 PM
    • Loading...
    • bleroy
    • Joined on 04-12-2003, 7:09 AM
    • Redmond
    • Posts 2,295
    • Points 13,293
    Hi,

    A kB is 1024 bytes, and a MB is 1024 kB. Do the math...

    Hope this helps.
    Bertrand
    ----
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: Format filesize in kB or MB

    08-08-2003, 4:21 PM
    • Loading...
    • bdesmet
    • Joined on 08-04-2002, 10:39 AM
    • Belgium
    • Posts 1,651
    • Points 8,255
    You can use this piece of C# code to do this:
    using System;
    
    class Test
    {
    public static void Main(string[]args)
    {
    Console.WriteLine(GetSize(int.Parse(args[0])));
    }

    public static string GetSize(long size)
    {
    double s = size;

    string[] format = new string[] {"{0} bytes", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB"};

    int i = 0;
    while (i < format.Length && s >= 1024) {
    s = (int) (100 * s / 1024) / 100.0;
    i++;
    }

    return string.Format(format[i],s);
    }
    }
    Bart De Smet [MVP]



    Visit www.msdn.be, www.bartdesmet.net
  • Re: Format filesize in kB or MB

    07-30-2008, 8:52 AM
    • Loading...
    • alperdotnet
    • Joined on 07-22-2008, 7:12 PM
    • istanbul
    • Posts 2
    • Points 4

    I use to same code. 

    Public Function BytesToMegabytes(Bytes As Double) As Double
       'This function gives an estimate to two decimal
       'places.  For a more precise answer, format to
       'more decimal places or just return dblAns
     
      Dim dblAns As Double
      dblAns = (Bytes / 1024) / 1024
      BytesToMegabytes = Format(dblAns, "###,###,##0.00")
     
    End Function

Page 1 of 1 (5 items)