[PTRun][UnitConverter]Preserve more significant digits (#35073)

* [PTRun][UnitConverter]Preserve more significant digits

* Use StringComparison.OrdinalIgnoreCase
This commit is contained in:
PesBandi
2024-10-18 15:51:57 +02:00
committed by GitHub
parent dd5cd2a570
commit e99b52fb35
4 changed files with 28 additions and 66 deletions

View File

@@ -2,13 +2,16 @@
// 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 UnitsNet;
namespace Community.PowerToys.Run.Plugin.UnitConverter
{
public class UnitConversionResult
{
public static string Format { get; set; } = "g14";
public static string TitleFormat { get; set; } = "G14";
public static string CopyFormat { get; set; } = "R";
public double ConvertedValue { get; }
@@ -23,14 +26,25 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
QuantityInfo = quantityInfo;
}
public string ToString(System.IFormatProvider provider = null)
public string ToString(IFormatProvider provider = null)
{
if (provider == null)
{
provider = System.Globalization.CultureInfo.CurrentCulture;
}
return ConvertedValue.ToString(Format, provider) + " " + UnitName;
// Check if the formatted number matches the original value. If they differ, some
// decimal places where cut off, and therefore we add an ellipsis.
string formatted = ConvertedValue.ToString(TitleFormat, provider);
if (double.TryParse(formatted, provider, out double parsedNumber) &&
Math.Abs(ConvertedValue - parsedNumber) > double.Epsilon &&
!formatted.Contains('E', StringComparison.OrdinalIgnoreCase))
{
return formatted + "… " + UnitName;
}
return formatted + " " + UnitName;
}
}
}