DotNet specifies the file display size, dotnet specifies the size

Source: Internet
Author: User
Tags dotnet

DotNet specifies the file display size, dotnet specifies the size

During project development, you sometimes need to control the file size, for example, you need to specify the file size as byte, TB, and so on. Now we provide a method to specify the file size,

Provides: "Byte", "KB", "MB", "GB", "TB" and other size control options. The following code can be directly used in the project, improve project development efficiency.

[System. componentModel. editorBrowsable (System. componentModel. editorBrowsableState. never)] public static class FsLibValueExtract {readonly static string [] SizeDefinitions = {"Byte", "KB", "MB", "GB", "TB "}; /// <summary> // control the upper limit of Size Display conversion /// </summary> readonly static double SizeLevel = 0x400*0.9; /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this double size) {return ToSizeDescription (size, 2 );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> size display mode </returns> public static string ToSizeDescription (this double size, int digits) {var sizeDefine = 0; while (sizeDefine <SizeDefinitions. length & size> SizeLevel) {size/= 0x400; sizeDefine ++;} if (sizeDefine = 0) return size. toString ("#0") + "" + SizeDefinitions [sizeDefine]; return size. toString ("#0. "+ string. empty. padLeft (digits, '#') + "" + SizeDefinitions [sizeDefine];} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this ulong size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this ulong size, int digits) {return (double) size ). toSizeDescription (digits );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this long size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this long size, int digits) {return (double) size ). toSizeDescription (digits );} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <returns> size display mode </returns> public static string ToSizeDescription (this int size) {return (double) size ). toSizeDescription ();} /// <summary> /// convert to the Dimension Display Mode. /// </summary> /// <param name = "size"> size </param> /// <param name = "digits"> decimal places </param> // <returns> display mode </returns> public static string ToSizeDescription (this int size, int digits) {return (double) size ). toSizeDescription (digits );}}

The above is only an arithmetic operation on the file size to implement the functions described above.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.