mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
[PTRun][UnitConverter]Increase float number precision (#17758)
* [PT Run] UnitConverter float number precision is not enough Introduced rounding to significant digits, not to digits after decimal separator Added conversion to string to fix last digit errors * [PT Run] UnitConverter float number precision is not enough spell check fixes * [PT Run] UnitConverter float number precision is not enough renamed test method to HandleNanometerToKilometer * [PT Run] UnitConverter float number precision is not enough result copied to clipboard will not have unit, just a number
This commit is contained in:
@@ -26,6 +26,14 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
|
||||
Assert.AreEqual(100, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HandleNanometerToKilometer()
|
||||
{
|
||||
var convertModel = new ConvertModel(1, "nanometer", "kilometer");
|
||||
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.QuantityType.Length);
|
||||
Assert.AreEqual(1E-12, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HandlesByteCapitals()
|
||||
{
|
||||
@@ -34,6 +42,26 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
|
||||
Assert.AreEqual(8, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HandlesParsecToNanometer()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HandlesNanometerToParsec()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void HandleInvalidModel()
|
||||
{
|
||||
@@ -41,5 +69,47 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
|
||||
var results = UnitHandler.Convert(convertModel);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
return new Result
|
||||
{
|
||||
ContextData = result,
|
||||
Title = $"{result.ConvertedValue} {result.UnitName}",
|
||||
Title = result.ToString(),
|
||||
IcoPath = _icon_path,
|
||||
Score = 300,
|
||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.copy_to_clipboard, result.QuantityType),
|
||||
@@ -73,7 +73,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(CultureInfo.CurrentCulture));
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
@@ -105,7 +105,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(CultureInfo.CurrentCulture));
|
||||
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
public class UnitConversionResult
|
||||
{
|
||||
public static string Format { get; set; } = "g14";
|
||||
|
||||
public double ConvertedValue { get; }
|
||||
|
||||
public string UnitName { get; }
|
||||
@@ -20,5 +22,15 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
UnitName = unitName;
|
||||
QuantityType = quantityType;
|
||||
}
|
||||
|
||||
public string ToString(System.IFormatProvider provider = null)
|
||||
{
|
||||
if (provider == null)
|
||||
{
|
||||
provider = System.Globalization.CultureInfo.CurrentCulture;
|
||||
}
|
||||
|
||||
return ConvertedValue.ToString(Format, provider) + " " + UnitName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,23 @@ 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, _roundingFractionalDigits) * exponent;
|
||||
return rounded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
|
||||
/// </summary>
|
||||
@@ -83,7 +100,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
|
||||
if (!double.IsNaN(convertedValue))
|
||||
{
|
||||
UnitConversionResult result = new UnitConversionResult(Math.Round(convertedValue, _roundingFractionalDigits), convertModel.ToUnit, quantityType);
|
||||
UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityType);
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user