Newer
Older
Correlator / PipeGallery / Manage / Utils.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PipeGallery.Manage
{

    public class Utils
    {

        /// <summary>
        /// 字节转字符串大小计算
        /// </summary>
        /// <returns></returns>
        public static string BytesToStringSize(double bytes)
        {
            if (bytes > 1024 * 1024 * 1024)
            {
                bytes = bytes / (1024 * 1024 * 1024);
                return string.Format("{0} GB", bytes.ToString("0.00"));
            }
            else if (bytes > 1024 * 1024)
            {
                bytes = bytes / (1024 * 1024);
                return string.Format("{0} MB", bytes.ToString("0.00"));
            }
            else if (bytes > 1024)
            {
                bytes = bytes / (1024);
                return string.Format("{0} KB", bytes.ToString("0.00"));
            }
            else
            {
                return string.Format("{0} B", bytes.ToString("0.00"));
            }
        }

        /// <summary>
        /// 计算速度转字符串
        /// </summary>
        public static string BytesToStringSpeed(double bytes)
        {
            double tmpSpeed = (bytes * 1000.0) / 1000;

            if (tmpSpeed == 0.0) tmpSpeed = 0;
            else
            {
                if (!double.TryParse(tmpSpeed.ToString(), out tmpSpeed))
                {
                    tmpSpeed = 0;
                }
            }
            if (tmpSpeed > 1024 * 1024)
            {
                tmpSpeed = tmpSpeed / (1024 * 1024);
                return string.Format("{0} MB/s", tmpSpeed.ToString("0.00"));
            }
            else if (tmpSpeed > 1024)
            {
                tmpSpeed = tmpSpeed / (1024);
                return string.Format("{0} KB/s", tmpSpeed.ToString("0.00"));
            }
            else
            {
                return string.Format("{0} B/s", tmpSpeed.ToString("0.00"));
            }
        }

    }
}