mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +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:
@@ -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