From e99b52fb35275d3259b41fb0c775cc081856c8f8 Mon Sep 17 00:00:00 2001
From: PesBandi <127593627+PesBandi@users.noreply.github.com>
Date: Fri, 18 Oct 2024 15:51:57 +0200
Subject: [PATCH] [PTRun][UnitConverter]Preserve more significant digits
(#35073)
* [PTRun][UnitConverter]Preserve more significant digits
* Use StringComparison.OrdinalIgnoreCase
---
.../UnitHandlerTests.cs | 49 +++----------------
.../Main.cs | 4 +-
.../UnitConversionResult.cs | 20 ++++++--
.../UnitHandler.cs | 21 +-------
4 files changed, 28 insertions(+), 66 deletions(-)
diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter.UnitTest/UnitHandlerTests.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter.UnitTest/UnitHandlerTests.cs
index 0789d29a3c..bab0199b27 100644
--- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter.UnitTest/UnitHandlerTests.cs
+++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter.UnitTest/UnitHandlerTests.cs
@@ -57,8 +57,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
var convertModel = new ConvertModel(1, "parsec", "nanometer");
var result = UnitHandler.Convert(convertModel).Single();
var str = result.ToString(System.Globalization.CultureInfo.InvariantCulture);
- Assert.AreEqual(3.0857000000000004E+25, result.ConvertedValue);
- Assert.AreEqual("3.0857e+25 nanometer", str);
+ Assert.AreEqual(3.08567758128E+25, result.ConvertedValue);
+ Assert.AreEqual("3.08567758128E+25 nanometer", str);
}
[TestMethod]
@@ -67,8 +67,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
var convertModel = new ConvertModel(1, "nanometer", "parsec");
var result = UnitHandler.Convert(convertModel).Single();
var str = result.ToString(System.Globalization.CultureInfo.InvariantCulture);
- Assert.AreEqual(3.2408000000000005E-26, result.ConvertedValue);
- Assert.AreEqual("3.2408e-26 parsec", str);
+ Assert.AreEqual(3.240779289666357E-26, result.ConvertedValue);
+ Assert.AreEqual("3.2407792896664E-26 parsec", str);
}
[TestMethod]
@@ -79,46 +79,13 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
Assert.AreEqual(0, results.Count());
}
- [TestMethod]
- public void RoundZero()
- {
- double result = UnitHandler.Round(0.0);
- Assert.AreEqual(0, result);
- }
-
[TestMethod]
public void RoundNormalValue()
{
- double result = UnitHandler.Round(3.141592653589793);
- Assert.AreEqual(3.1416, result);
- }
-
- [TestMethod]
- public void RoundSmallValue()
- {
- double result = UnitHandler.Round(1.23456789012345E-16);
- Assert.AreEqual(1.2346E-16, result);
- }
-
- [TestMethod]
- public void RoundBigValue()
- {
- double result = UnitHandler.Round(1234567890123456.0);
- Assert.AreEqual(1234600000000000.0, result);
- }
-
- [TestMethod]
- public void RoundNegativeValue()
- {
- double result = UnitHandler.Round(-3.141592653589793);
- Assert.AreEqual(-3.1416, result);
- }
-
- [TestMethod]
- public void RoundNinesValue()
- {
- double result = UnitHandler.Round(999999999999.9998);
- Assert.AreEqual(1000000000000.0, result);
+ var convertModel = new ConvertModel(3.14159265358979323, "stone", "kg");
+ var result = UnitHandler.Convert(convertModel).Single();
+ var str = result.ToString(System.Globalization.CultureInfo.InvariantCulture);
+ Assert.AreEqual("19.950018128979… kg", str);
}
}
}
diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs
index 1d471420b4..bc4c00d7a7 100644
--- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs
+++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/Main.cs
@@ -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)
diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitConversionResult.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitConversionResult.cs
index 9c5c94c69e..576d677387 100644
--- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitConversionResult.cs
+++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitConversionResult.cs
@@ -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;
}
}
}
diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs
index d8c0dce3c6..fe6b4634dd 100644
--- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs
+++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.UnitConverter/UnitHandler.cs
@@ -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;
}
- ///
- /// Rounds the value to the predefined number of significant digits.
- ///
- /// Value to be rounded
- 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;
- }
-
///
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
///
@@ -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);
}
}