mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +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);
|
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]
|
[TestMethod]
|
||||||
public void HandlesByteCapitals()
|
public void HandlesByteCapitals()
|
||||||
{
|
{
|
||||||
@@ -34,6 +42,26 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
|
|||||||
Assert.AreEqual(8, result);
|
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]
|
[TestMethod]
|
||||||
public void HandleInvalidModel()
|
public void HandleInvalidModel()
|
||||||
{
|
{
|
||||||
@@ -41,5 +69,47 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
|
|||||||
var results = UnitHandler.Convert(convertModel);
|
var results = UnitHandler.Convert(convertModel);
|
||||||
Assert.AreEqual(0, results.Count());
|
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
|
return new Result
|
||||||
{
|
{
|
||||||
ContextData = result,
|
ContextData = result,
|
||||||
Title = $"{result.ConvertedValue} {result.UnitName}",
|
Title = result.ToString(),
|
||||||
IcoPath = _icon_path,
|
IcoPath = _icon_path,
|
||||||
Score = 300,
|
Score = 300,
|
||||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.copy_to_clipboard, result.QuantityType),
|
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.copy_to_clipboard, result.QuantityType),
|
||||||
@@ -73,7 +73,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Clipboard.SetText(result.ConvertedValue.ToString(CultureInfo.CurrentCulture));
|
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
catch (ExternalException)
|
catch (ExternalException)
|
||||||
@@ -105,7 +105,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Clipboard.SetText(result.ConvertedValue.ToString(CultureInfo.CurrentCulture));
|
Clipboard.SetText(result.ConvertedValue.ToString(UnitConversionResult.Format, CultureInfo.CurrentCulture));
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
catch (ExternalException)
|
catch (ExternalException)
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
|||||||
{
|
{
|
||||||
public class UnitConversionResult
|
public class UnitConversionResult
|
||||||
{
|
{
|
||||||
|
public static string Format { get; set; } = "g14";
|
||||||
|
|
||||||
public double ConvertedValue { get; }
|
public double ConvertedValue { get; }
|
||||||
|
|
||||||
public string UnitName { get; }
|
public string UnitName { get; }
|
||||||
@@ -20,5 +22,15 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
|||||||
UnitName = unitName;
|
UnitName = unitName;
|
||||||
QuantityType = quantityType;
|
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;
|
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>
|
/// <summary>
|
||||||
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
|
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -83,7 +100,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
|
|||||||
|
|
||||||
if (!double.IsNaN(convertedValue))
|
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);
|
results.Add(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user