在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
语法注意点
Humanizer(人性化)Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
string// Return a string describing the value as a file size. // For example, 1.23 MB. public static string ToFileSize(this double value) { string[] suffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; for (int i = 0; i < suffixes.Length; i++) { if (value <= (Math.Pow(1024, i + 1))) { return ThreeNonZeroDigits(value / Math.Pow(1024, i)) + " " + suffixes[i]; } } return ThreeNonZeroDigits(value / Math.Pow(1024, suffixes.Length - 1)) + " " + suffixes[suffixes.Length - 1]; } // Return the value formatted to include at most three // non-zero digits and at most two digits after the // decimal point. Examples: // 1 // 123 // 12.3 // 1.23 // 0.12 private static string ThreeNonZeroDigits(double value) { if (value >= 100) { // No digits after the decimal. return value.ToString("0,0"); } else if (value >= 10) { // One digit after the decimal. return value.ToString("0.0"); } else { // Two digits after the decimal. return value.ToString("0.00"); } }
参考资料 扩展方法 (C# 编程指南) (Entrance on MSDN)
c# 扩展方法奇思妙用
Conversion rules for Instance parameters and their impact
Extension methods Interoperability between languages
开源扩展项目
|
请发表评论