mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
[Peek]Fix file and folder sizes to be similar to Explorer (#28089)
* [Peek] displaying file size in correct grammatical format * Update Directory.Packages.props * removed unnecessary file * [Peek] Added new symbols in resources.resw * [Peek] added commas separation * modified spell check * [Peek] Changed 1024 factor to 1000 factor * modified spell check * made string country specific * fix: used 1024 and fixed precision * spellcheck fixed * Update src/modules/peek/Peek.UI/Strings/en-us/Resources.resw * Update src/modules/peek/Peek.UI/Strings/en-us/Resources.resw
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Peek.Common.Helpers
|
||||
{
|
||||
public static class MathHelper
|
||||
@@ -10,5 +13,10 @@ namespace Peek.Common.Helpers
|
||||
{
|
||||
return a < 0 ? ((a % b) + b) % b : a % b;
|
||||
}
|
||||
|
||||
public static int NumberOfDigits(int num)
|
||||
{
|
||||
return Math.Abs(num).ToString(CultureInfo.InvariantCulture).Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,34 +10,40 @@ namespace Peek.Common.Helpers
|
||||
{
|
||||
public static class ReadableStringHelper
|
||||
{
|
||||
private const int DecimalPercision = 10;
|
||||
private const int MaxDigitsToDisplay = 3;
|
||||
private const int PowerFactor = 1024;
|
||||
|
||||
public static string BytesToReadableString(ulong bytes)
|
||||
{
|
||||
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
||||
List<string> format = new List<string>
|
||||
{
|
||||
(bytes == 1) ?
|
||||
resourceLoader.GetString("ReadableString_ByteAbbreviationFormat") : // "byte"
|
||||
resourceLoader.GetString("ReadableString_BytesAbbreviationFormat"), // "bytes"
|
||||
resourceLoader.GetString("ReadableString_KiloByteAbbreviationFormat"), // "KB"
|
||||
resourceLoader.GetString("ReadableString_MegaByteAbbreviationFormat"), // "MB"
|
||||
resourceLoader.GetString("ReadableString_GigaByteAbbreviationFormat"), // "GB"
|
||||
resourceLoader.GetString("ReadableString_TeraByteAbbreviationFormat"), // "TB"
|
||||
resourceLoader.GetString("ReadableString_PetaByteAbbreviationFormat"), // "PB"
|
||||
resourceLoader.GetString("ReadableString_ExaByteAbbreviationFormat"), // "EB"
|
||||
};
|
||||
string totalBytesDisplays = (bytes == 1) ?
|
||||
ResourceLoaderInstance.ResourceLoader.GetString("ReadableString_ByteString") :
|
||||
ResourceLoaderInstance.ResourceLoader.GetString("ReadableString_BytesString");
|
||||
|
||||
int index = 0;
|
||||
double number = 0.0;
|
||||
|
||||
if (bytes > 0)
|
||||
{
|
||||
index = (int)Math.Floor(Math.Log(bytes) / Math.Log(1024));
|
||||
number = Math.Round((bytes / Math.Pow(1024, index)) * DecimalPercision) / DecimalPercision;
|
||||
index = (int)Math.Floor(Math.Log(bytes) / Math.Log(PowerFactor));
|
||||
number = bytes / Math.Pow(PowerFactor, index);
|
||||
}
|
||||
|
||||
return string.Format(CultureInfo.InvariantCulture, format[index], number);
|
||||
if (index > 0 && number >= Math.Pow(10, MaxDigitsToDisplay))
|
||||
{
|
||||
index++;
|
||||
number = bytes / Math.Pow(PowerFactor, index);
|
||||
}
|
||||
|
||||
int precision = GetPrecision(index, number);
|
||||
int decimalPrecision = (int)Math.Pow(10, precision);
|
||||
|
||||
number = Math.Truncate(number * decimalPrecision) / decimalPrecision;
|
||||
|
||||
string formatSpecifier = GetFormatSpecifierString(index, number, bytes, precision);
|
||||
|
||||
return bytes == 0
|
||||
? string.Format(CultureInfo.CurrentCulture, formatSpecifier, number)
|
||||
: string.Format(CultureInfo.CurrentCulture, formatSpecifier + totalBytesDisplays, number, bytes);
|
||||
}
|
||||
|
||||
public static string FormatResourceString(string resourceId, object? args)
|
||||
@@ -55,5 +61,32 @@ namespace Peek.Common.Helpers
|
||||
|
||||
return formattedString;
|
||||
}
|
||||
|
||||
public static int GetPrecision(int index, double number)
|
||||
{
|
||||
int numberOfDigits = MathHelper.NumberOfDigits((int)number);
|
||||
return index == 0 ?
|
||||
0 :
|
||||
MaxDigitsToDisplay - numberOfDigits;
|
||||
}
|
||||
|
||||
public static string GetFormatSpecifierString(int index, double number, ulong bytes, int precision)
|
||||
{
|
||||
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
||||
List<string> format = new List<string>
|
||||
{
|
||||
(bytes == 1) ?
|
||||
resourceLoader.GetString("ReadableString_ByteAbbreviationFormat") : // "byte"
|
||||
resourceLoader.GetString("ReadableString_BytesAbbreviationFormat"), // "bytes"
|
||||
resourceLoader.GetString("ReadableString_KiloByteAbbreviationFormat"), // "KB"
|
||||
resourceLoader.GetString("ReadableString_MegaByteAbbreviationFormat"), // "MB"
|
||||
resourceLoader.GetString("ReadableString_GigaByteAbbreviationFormat"), // "GB"
|
||||
resourceLoader.GetString("ReadableString_TeraByteAbbreviationFormat"), // "TB"
|
||||
resourceLoader.GetString("ReadableString_PetaByteAbbreviationFormat"), // "PB"
|
||||
resourceLoader.GetString("ReadableString_ExaByteAbbreviationFormat"), // "EB"
|
||||
};
|
||||
|
||||
return "{0:F" + precision + "} " + format[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user