mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
[PTRun][UnitConverter]Preserve more significant digits (#35073)
* [PTRun][UnitConverter]Preserve more significant digits * Use StringComparison.OrdinalIgnoreCase
This commit is contained in:
@@ -72,7 +72,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.CopyFormat, CultureInfo.CurrentCulture));
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
@@ -104,7 +104,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.CopyFormat, CultureInfo.CurrentCulture));
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
public static class UnitHandler
|
||||
{
|
||||
private static readonly int _roundingSignificantDigits = 4;
|
||||
|
||||
private static readonly QuantityInfo[] _included = new QuantityInfo[]
|
||||
{
|
||||
Acceleration.Info,
|
||||
@@ -59,23 +57,6 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the value to the predefined number of significant digits.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to be rounded</param>
|
||||
public static double Round(double value)
|
||||
{
|
||||
if (value == 0.0D)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var power = Math.Floor(Math.Log10(Math.Abs(value)));
|
||||
var exponent = Math.Pow(10, power);
|
||||
var rounded = Math.Round(value / exponent, _roundingSignificantDigits) * exponent;
|
||||
return rounded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
|
||||
/// </summary>
|
||||
@@ -106,7 +87,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
|
||||
if (!double.IsNaN(convertedValue))
|
||||
{
|
||||
UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityInfo);
|
||||
UnitConversionResult result = new UnitConversionResult(convertedValue, convertModel.ToUnit, quantityInfo);
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user