20211102
This commit is contained in:
parent
3ee973d911
commit
ed50c4a0cc
78
anbs_cp/FileSizeFormatter.cs
Normal file
78
anbs_cp/FileSizeFormatter.cs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
using System;
|
||||||
|
namespace anbs_cp
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Форматирует размер файла/папки в понятную строку
|
||||||
|
/// </summary>
|
||||||
|
public static class FileSizeFormatter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Имена размеров (байт, килобайт, мегабайт, гигабайт и террабайт)
|
||||||
|
/// </summary>
|
||||||
|
public static string[] SizeNames { get; set; } = { "Байт", "Кб", "Мб", "Гб", "Тб" };
|
||||||
|
/// <summary>
|
||||||
|
/// Знаков после запятой
|
||||||
|
/// </summary>
|
||||||
|
public static byte DecimalPlaces { get; set; } = 2;
|
||||||
|
/// <summary>
|
||||||
|
/// Максимально байт (далее идут Кбайты)
|
||||||
|
/// </summary>
|
||||||
|
public static long ByteMax { get; set; } = 1024;
|
||||||
|
/// <summary>
|
||||||
|
/// Максимально Кбайт (далее идут Мбайты)
|
||||||
|
/// </summary>
|
||||||
|
public static long KByteMax { get; set; } = 1048576;
|
||||||
|
/// <summary>
|
||||||
|
/// Максимально Мбайт (далее идут Гбайты)
|
||||||
|
/// </summary>
|
||||||
|
public static long MByteMax { get; set; } = 1073741824;
|
||||||
|
/// <summary>
|
||||||
|
/// Максимально Гбайт (далее идут Тбайты)
|
||||||
|
/// </summary>
|
||||||
|
public static long GByteMax { get; set; } = 1099511627776;
|
||||||
|
/// <summary>
|
||||||
|
/// Форматирование размера файла
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">Размер файла в Байтах</param>
|
||||||
|
/// <returns>Форматированный размер файла (например, 20 Мб)</returns>
|
||||||
|
public static string Format(long value)
|
||||||
|
{
|
||||||
|
//Bytes
|
||||||
|
if (value < ByteMax)
|
||||||
|
return $"{value} {SizeNames[0]}";
|
||||||
|
//KiloBytes
|
||||||
|
if ((value >= ByteMax) && (value < KByteMax))
|
||||||
|
return $"{FrmtSize(value, ByteMax)} {SizeNames[1]}";
|
||||||
|
//MegaBytes
|
||||||
|
if ((value >= KByteMax) && (value < MByteMax))
|
||||||
|
return $"{FrmtSize(value, KByteMax)} {SizeNames[2]}";
|
||||||
|
//GigaBytes
|
||||||
|
if ((value >= MByteMax) && (value < GByteMax))
|
||||||
|
return $"{FrmtSize(value, MByteMax)} {SizeNames[3]}";
|
||||||
|
//TeraBytes
|
||||||
|
if (value >= GByteMax)
|
||||||
|
return $"{FrmtSize(value, GByteMax)} {SizeNames[4]}";
|
||||||
|
//Не определён
|
||||||
|
return $"{value} {SizeNames[0]}";
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Деление числа на число с DecimalPlaces знаками после запятой
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dividend">Делимое число</param>
|
||||||
|
/// <param name="divider">Число-делитель</param>
|
||||||
|
/// <returns>Частное (с DecimalPlaces знаками после запятой)</returns>
|
||||||
|
private static string FrmtSize(long dividend, long divider)
|
||||||
|
{
|
||||||
|
long delim = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i <= DecimalPlaces; i++)
|
||||||
|
{
|
||||||
|
delim *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
decimal value = Math.Round((decimal)(dividend * delim / divider)) / delim;
|
||||||
|
|
||||||
|
return $"{value}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,19 +4,19 @@
|
|||||||
{
|
{
|
||||||
public static string IntToStr(int AInt) => AInt.ToString();
|
public static string IntToStr(int AInt) => AInt.ToString();
|
||||||
public static string IntToStr(long AInt) => AInt.ToString();
|
public static string IntToStr(long AInt) => AInt.ToString();
|
||||||
public static int StrToInt(string AStr)
|
public static int StrToInt(string AStr, int ADefault = 0)
|
||||||
{
|
{
|
||||||
if (!int.TryParse(AStr, out int result))
|
if (!int.TryParse(AStr, out int result))
|
||||||
{
|
{
|
||||||
result = 0;
|
result = ADefault;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static long StrToInt64(string AStr)
|
public static long StrToInt64(string AStr, long ADefault = 0)
|
||||||
{
|
{
|
||||||
if (!long.TryParse(AStr, out long result))
|
if (!long.TryParse(AStr, out long result))
|
||||||
{
|
{
|
||||||
result = 0;
|
result = ADefault;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user