Write bytes, KB, MB, GB, TB message. 1 KB = 1024 Bytes 1 MB = 1024 KB = 1048576 Bytes 1 GB = 1024 MB = 1073741824 Bytes 1 TB = 1024 GB = 1099511627776 Bytes E.g. 100 will return "100 Bytes" 2048 will return "2.00 KB" 2500 will return "2.44 KB" 1534905 will return "1.46 MB" 23045904850904 will return "20.96 TB" - CSharp System

CSharp examples for System:Byte

Description

Write bytes, KB, MB, GB, TB message. 1 KB = 1024 Bytes 1 MB = 1024 KB = 1048576 Bytes 1 GB = 1024 MB = 1073741824 Bytes 1 TB = 1024 GB = 1099511627776 Bytes E.g. 100 will return "100 Bytes" 2048 will return "2.00 KB" 2500 will return "2.44 KB" 1534905 will return "1.46 MB" 23045904850904 will return "20.96 TB"

Demo Code


using System.Text;
using System.Globalization;
using System.Collections.Specialized;
using System.Collections;
using System;/*from  ww w.  ja  v a  2s .c  o  m*/

public class Main{
        /// <summary>
        /// Write bytes, KB, MB, GB, TB message.
        /// 1 KB = 1024 Bytes
        /// 1 MB = 1024 KB = 1048576 Bytes
        /// 1 GB = 1024 MB = 1073741824 Bytes
        /// 1 TB = 1024 GB = 1099511627776 Bytes
        /// E.g. 100 will return "100 Bytes"
        /// 2048 will return "2.00 KB"
        /// 2500 will return "2.44 KB"
        /// 1534905 will return "1.46 MB"
        /// 23045904850904 will return "20.96 TB"
        /// </summary>
        public static string WriteBigByteNumber( long bigByteNumber )
        {
            string decimalSeperator = CultureInfo.CurrentCulture.
                NumberFormat.CurrencyDecimalSeparator;
            return WriteBigByteNumber( bigByteNumber, decimalSeperator );
        } // WriteBigByteNumber(num)
        #endregion

        #region Kb/mb name generator
        /// <summary>
        /// Write bytes, KB, MB, GB, TB message.
        /// 1 KB = 1024 Bytes
        /// 1 MB = 1024 KB = 1048576 Bytes
        /// 1 GB = 1024 MB = 1073741824 Bytes
        /// 1 TB = 1024 GB = 1099511627776 Bytes
        /// E.g. 100 will return "100 Bytes"
        /// 2048 will return "2.00 KB"
        /// 2500 will return "2.44 KB"
        /// 1534905 will return "1.46 MB"
        /// 23045904850904 will return "20.96 TB"
        /// </summary>
        public static string WriteBigByteNumber(
            long bigByteNumber, string decimalSeperator )
        {
            if ( bigByteNumber < 0 )
                return "-" + WriteBigByteNumber( -bigByteNumber );

            if ( bigByteNumber <= 999 )
                return bigByteNumber + " Bytes";
            if ( bigByteNumber <= 999 * 1024 )
            {
                double fKB = (double)bigByteNumber / 1024.0;
                return (int)fKB + decimalSeperator +
                    ( (int)( fKB * 100.0f ) % 100 ).ToString( "00" ) + " KB";
            } // if
            if ( bigByteNumber <= 999 * 1024 * 1024 )
            {
                double fMB = (double)bigByteNumber / ( 1024.0 * 1024.0 );
                return (int)fMB + decimalSeperator +
                    ( (int)( fMB * 100.0f ) % 100 ).ToString( "00" ) + " MB";
            } // if
            // this is very big ^ will not fit into int
            if ( bigByteNumber <= 999L * 1024L * 1024L * 1024L )
            {
                double fGB = (double)bigByteNumber / ( 1024.0 * 1024.0 * 1024.0 );
                return (int)fGB + decimalSeperator +
                    ( (int)( fGB * 100.0f ) % 100 ).ToString( "00" ) + " GB";
            } // if
            //if ( num <= 999*1024*1024*1024*1024 )
            //{
            double fTB = (double)bigByteNumber / ( 1024.0 * 1024.0 * 1024.0 * 1024.0 );
            return (int)fTB + decimalSeperator +
                ( (int)( fTB * 100.0f ) % 100 ).ToString( "00" ) + " TB";
            //} // if
        } // WriteBigByteNumber(num, decimalSeperator)
}

Related Tutorials