From 634681ce2bb36cd686943102a0f071cd6a09049f Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Wed, 27 Aug 2025 21:22:55 +1000 Subject: fixed bugs and revolutionized the universe --- RegionVisualiser/SizeFormatter.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 RegionVisualiser/SizeFormatter.cs (limited to 'RegionVisualiser/SizeFormatter.cs') diff --git a/RegionVisualiser/SizeFormatter.cs b/RegionVisualiser/SizeFormatter.cs new file mode 100644 index 0000000..3479bd8 --- /dev/null +++ b/RegionVisualiser/SizeFormatter.cs @@ -0,0 +1,25 @@ +namespace RegionVisualiser; + +public static class SizeFormatter +{ + private static readonly string[] Units = ["bytes", "KB", "MB", "GB", "TB"]; + + public static string HumanReadable(long bytes) + { + if (bytes < 1024) return $"{bytes} {SizeFormatter.Units[0]}"; + + double value = bytes; + int unit = 0; + + while (value >= 1024 && unit < SizeFormatter.Units.Length - 1) + { + value /= 1024; + unit++; + } + + // Show up to 2 decimal places if not whole + return value % 1 == 0 + ? $"{value:0} {SizeFormatter.Units[unit]}" + : $"{value:0.##} {SizeFormatter.Units[unit]}"; + } +} \ No newline at end of file -- cgit v1.3.1