[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:
lncubus
2022-04-19 12:20:12 +02:00
committed by GitHub
parent 38c538b0c5
commit da0aac2a18
4 changed files with 103 additions and 4 deletions

View File

@@ -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);
}
}