BetterExplorer.FriendlySizeConverter.GetFriendlySize C# (CSharp) Method

GetFriendlySize() public static method

Converts bytes into a user friendly string converted into larger units when desired.
public static GetFriendlySize ( double bytes ) : string
bytes double The number of bytes
return string
		public static string GetFriendlySize(double bytes) {
			if (bytes < 1000)
				return bytes.ToString() + " B";
			else if (bytes < 1000000)
				return Math.Round((bytes / 1000), 2, MidpointRounding.AwayFromZero).ToString() + " KB";
			else if (bytes < 1000000000)
				return Math.Round((bytes / 1000000), 2, MidpointRounding.AwayFromZero).ToString() + " MB";
			else
				return Math.Round((bytes / 1000000000), 2, MidpointRounding.AwayFromZero).ToString() + " GB";
		}
	}

Usage Example

        private void UpdateFileListFromBox()
        {
            int  tfc = 0;
            long tfs = 0;

            foreach (object item in listBox1.Items)
            {
                System.IO.FileInfo fd = new System.IO.FileInfo((string)item);
                tfc += 1;
                tfs += fd.Length;
            }

            if (tfc > 1)
            {
                this.radioButton3.Visible = false;
                this.radioButton4.Visible = false;
                this.radioButton6.Visible = false;
                label7.Visible            = true;
            }
            else
            {
                this.radioButton3.Visible = true;
                this.radioButton4.Visible = true;
                this.radioButton6.Visible = true;
                label7.Visible            = false;
            }

            this.lblCount.Text = Convert.ToString(tfc);
            this.lblSize.Text  = FriendlySizeConverter.GetFriendlySize(tfs);

            if (this.listBox1.SelectedItems.Count > 0)
            {
                this.button3.Enabled = true;
            }
            else
            {
                this.button3.Enabled = false;
            }

            if (tfc == 0)
            {
                if (hsa == false)
                {
                    MessageBox.Show("You have not selected any files. Please select some files and try again.", "Create Archive", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cos = true;
                }
                else
                {
                    MessageBox.Show("There are currently no files listed to be archived. Please add some files before continuing.", "Create Archive", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.wizardPage1.AllowNext = false;
                }
            }
            else
            {
                this.wizardPage1.AllowNext = true;
            }
        }