12345678910111213141516171819202122232425262728293031323334353637383940 |
- namespace FNZCM.Shared.Helpers
- {
- public static class HumanReadHelper
- {
- public static string NullOrEmptyEscape(this string me, string escape) => string.IsNullOrWhiteSpace(me) ? escape : me;
- public static string SecondToDur(this int sec) => ((int?)sec).SecondToDur();
- public static string SecondToDur(this int? sec)
- {
- if (sec.HasValue == false) return string.Empty;
- var ts = TimeSpan.FromSeconds(sec.Value);
- return Math.Floor(ts.TotalDays) >= 1
- ? $"{Math.Floor(ts.TotalDays):00}.{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}"
- : Math.Floor(ts.TotalHours) >= 1
- ? $"{Math.Floor(ts.TotalHours):00}:{ts.Minutes:00}:{ts.Seconds:00}"
- : $"{Math.Floor(ts.TotalMinutes):00}:{ts.Seconds:00}";
- }
- public static string BytesToFileSize(this long length) => ((long?)length).BytesToFileSize();
- public static string BytesToFileSize(this long? length)
- {
- if (length.HasValue == false) return string.Empty;
- string[] sizes = { "B", "KB", "MB", "GB", "TB" };
- double len = length.Value;
- int order = 0;
- while (len >= 1024 && order < sizes.Length - 1)
- {
- order++;
- len = len / 1024;
- }
- // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
- // show a single decimal place, and no space.
- string result = $"{len:000.00}{sizes[order]}";
- return result;
- }
- }
- }
|