locked
Convert from bytes to GB RRS feed

  • Question

  • I have this code :

    ulong FreeBytesAvailable;
                ulong TotalNumberOfBytes;
                ulong TotalNumberOfFreeBytes;
    
                bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes, out TotalNumberOfFreeBytes);
                if (!success)
                throw new System.ComponentModel.Win32Exception();
    
                label16.Text = (String.Format("{0}", FreeBytesAvailable + " GB"));

    And want to convert the output to GB
    How is that done easy and with 2 decimals?

    I have tryed to ude this code:

    public string FormatBytes(long bytes)
            {
                const int scale = 1024;
                string[] orders = new string[] { "GB", "MB", "KB", "Bytes" };
                long max = (long)Math.Pow(scale, orders.Length - 1);
    
                foreach (string order in orders)
                {
                    if (bytes > max)
                        return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
    
                    max /= scale;
                }
                return "0 Bytes";

    But i cant figur out how to use it right.

    Tuesday, February 7, 2017 1:02 AM

Answers

  • Hi,

    I have update my code . Please check it.

     private String convertSize(double size)
            {
                String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
    
                double mod = 1024.0;
    
                int i = 0;
    
                while (size >= mod)
                {
                    size /= mod;
                    i++;
                }
                return Math.Round(size,2) + units[i];//with 2 decimals
            }
    

    Usage:

    label16.Text = convertSize(Convert.ToDouble(label16.Text));

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jonas Carlsen Thursday, February 9, 2017 1:57 AM
    Thursday, February 9, 2017 1:20 AM

All replies

  • Hi Jonas,

    I don't understand what you are trying to do.

    Can you show some sample output you are trying to obtain?

    Tuesday, February 7, 2017 9:44 AM
  • Tuesday, February 7, 2017 9:56 AM
  • Hi Jonas,

    I don't understand what you are trying to do.

    Can you show some sample output you are trying to obtain?

    i want the marked output to be converted from byte value to GB value

    Tuesday, February 7, 2017 2:43 PM
  • Hi,

    Try below code:

     private String convertSize(double size)
            {
                String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
    
                double mod = 1024.0;
    
                int i = 0;
    
                while (size >= mod)
                {
                    size /= mod;
                    i++;
                }
                return Math.Round(size) + units[i];
            }

    Hope it can be helpful to you.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Marco Ceccon Tuesday, December 17, 2019 5:36 PM
    Wednesday, February 8, 2017 3:20 AM

  • i want the marked output to be converted from byte value to GB value

    Yes, Explorer uses StrFormatByteSizeW() in properties dialog boxes



    • Edited by Castorix31 Wednesday, February 8, 2017 9:00 AM
    Wednesday, February 8, 2017 8:59 AM
  • Hi,

    Try below code:

     private String convertSize(double size)
            {
                String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
    
                double mod = 1024.0;
    
                int i = 0;
    
                while (size >= mod)
                {
                    size /= mod;
                    i++;
                }
                return Math.Round(size) + units[i];
            }

    Hope it can be helpful to you.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Cant get it to work with my label16.text probety



    Wednesday, February 8, 2017 8:25 PM

  • i want the marked output to be converted from byte value to GB value

    Yes, Explorer uses StrFormatByteSizeW() in properties dialog boxes



    The thing is that the problem dont lay in the web browser but the C# program code.
    Wednesday, February 8, 2017 8:26 PM
  • The thing is that the problem dont lay in the web browser but the C# program code.

    Which web browser ?

    StrFormatByteSizeW() is the standard API to format bytes in C# or any other language

    For example, I get "2.23 GB" like in the MSDN page :

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern long StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, UInt32 cchBuf);
    
    Int64 nValue = 2400000000;
    StringBuilder sValue = new StringBuilder(128);
    StrFormatByteSizeW(nValue, sValue, (uint)sValue.Capacity);
    string sBytes = sValue.ToString();



    • Edited by Castorix31 Wednesday, February 8, 2017 11:08 PM
    Wednesday, February 8, 2017 10:45 PM
  • The thing is that the problem dont lay in the web browser but the C# program code.

    Which web browser ?

    StrFormatByteSizeW() is the standard API to format bytes in C# or any other language

    For example, I get "2.23 GB" like in the MSDN page :

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern long StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, UInt32 cchBuf);
    
    Int64 nValue = 2400000000;
    StringBuilder sValue = new StringBuilder(128);
    StrFormatByteSizeW(nValue, sValue, (uint)sValue.Capacity);
    string sBytes = sValue.ToString();



    Sorry..
    Mis-read your post.

    i tryed the code an just got a bunch of errors.

    Thursday, February 9, 2017 12:42 AM
  • Hi,

    I have update my code . Please check it.

     private String convertSize(double size)
            {
                String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
    
                double mod = 1024.0;
    
                int i = 0;
    
                while (size >= mod)
                {
                    size /= mod;
                    i++;
                }
                return Math.Round(size,2) + units[i];//with 2 decimals
            }
    

    Usage:

    label16.Text = convertSize(Convert.ToDouble(label16.Text));

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jonas Carlsen Thursday, February 9, 2017 1:57 AM
    Thursday, February 9, 2017 1:20 AM
  • For example, I get "2.23 GB" like in the MSDN page :

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern long StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, UInt32 cchBuf);
    
    Int64 nValue = 2400000000;
    StringBuilder sValue = new StringBuilder(128);
    StrFormatByteSizeW(nValue, sValue, (uint)sValue.Capacity);
    string sBytes = sValue.ToString();



    Sorry..
    Mis-read your post.

    i tryed the code an just got a bunch of errors.

    where ?
    It works perfectly on my PC
    Thursday, February 9, 2017 1:25 AM
  • Hi,

    I have update my code . Please check it.

     private String convertSize(double size)
            {
                String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" };
    
                double mod = 1024.0;
    
                int i = 0;
    
                while (size >= mod)
                {
                    size /= mod;
                    i++;
                }
                return Math.Round(size,2) + units[i];//with 2 decimals
            }
    

    Usage:

    label16.Text = convertSize(Convert.ToDouble(label16.Text));

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    It works perfectly now.

    Thanks for the help

    Thursday, February 9, 2017 1:58 AM
  • For example, I get "2.23 GB" like in the MSDN page :

    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern long StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, UInt32 cchBuf);
    
    Int64 nValue = 2400000000;
    StringBuilder sValue = new StringBuilder(128);
    StrFormatByteSizeW(nValue, sValue, (uint)sValue.Capacity);
    string sBytes = sValue.ToString();

    where ?
    It works perfectly on my PC

    My label16 only prints the Bytes value.

    But the code parts in Bobs post works perfectly and did the trick

    Thursday, February 9, 2017 2:00 AM