mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
Adjusting to force all instances to use rounded vs result due to Mage's quirks (#7164)
This commit is contained in:
@@ -58,7 +58,9 @@ namespace Microsoft.Plugin.Calculator.UnitTests
|
|||||||
[TestCase("1 - 9.0 / 10", 0.1D)]
|
[TestCase("1 - 9.0 / 10", 0.1D)]
|
||||||
[TestCase("0.5 * ((2*-395.2)+198.2)", -296.1D)]
|
[TestCase("0.5 * ((2*-395.2)+198.2)", -296.1D)]
|
||||||
[TestCase("2+2.11", 4.11D)]
|
[TestCase("2+2.11", 4.11D)]
|
||||||
public void Interpret_NoErrors_WhenCalled(string input, decimal expectedResult)
|
[TestCase("8.43 + 4.43 - 12.86", 0D)]
|
||||||
|
[TestCase("8.43 + 4.43 - 12.8", 0.06D)]
|
||||||
|
public void Interpret_NoErrors_WhenCalledWithRounding(string input, decimal expectedResult)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var engine = new CalculateEngine();
|
var engine = new CalculateEngine();
|
||||||
@@ -68,7 +70,7 @@ namespace Microsoft.Plugin.Calculator.UnitTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.IsNotNull(result);
|
Assert.IsNotNull(result);
|
||||||
Assert.AreEqual(expectedResult, result.Result);
|
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("0.100000000000000000000", 0.00776627963145224D)] // BUG: Because data structure
|
[TestCase("0.100000000000000000000", 0.00776627963145224D)] // BUG: Because data structure
|
||||||
@@ -101,7 +103,7 @@ namespace Microsoft.Plugin.Calculator.UnitTests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.IsNotNull(result);
|
Assert.IsNotNull(result);
|
||||||
Assert.AreEqual(expectedResult, result.Result);
|
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("ceil(2 * (pi ^ 2))", true)]
|
[TestCase("ceil(2 * (pi ^ 2))", true)]
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
var decimalResult = Convert.ToDecimal(result, cultureInfo);
|
var decimalResult = Convert.ToDecimal(result, cultureInfo);
|
||||||
var roundedResult = Math.Round(decimalResult, RoundingDigits, MidpointRounding.AwayFromZero);
|
var roundedResult = Round(decimalResult);
|
||||||
|
|
||||||
return new CalculateResult()
|
return new CalculateResult()
|
||||||
{
|
{
|
||||||
@@ -50,6 +50,11 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static decimal Round(decimal value)
|
||||||
|
{
|
||||||
|
return Math.Round(value, RoundingDigits, MidpointRounding.AwayFromZero);
|
||||||
|
}
|
||||||
|
|
||||||
private static object TransformResult(object result)
|
private static object TransformResult(object result)
|
||||||
{
|
{
|
||||||
if (result.ToString() == "NaN")
|
if (result.ToString() == "NaN")
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
|
|
||||||
return new List<Result>
|
return new List<Result>
|
||||||
{
|
{
|
||||||
ResultHelper.CreateResult(result.Result, result.RoundedResult, IconPath),
|
ResultHelper.CreateResult(result.RoundedResult, IconPath),
|
||||||
};
|
};
|
||||||
} // We want to keep the process alive if any the mages library throws any exceptions.
|
} // We want to keep the process alive if any the mages library throws any exceptions.
|
||||||
#pragma warning disable CA1031 // Do not catch general exception types
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
{
|
{
|
||||||
public static Result CreateResult(CalculateResult result, string iconPath)
|
public static Result CreateResult(CalculateResult result, string iconPath)
|
||||||
{
|
{
|
||||||
return CreateResult(result.Result, result.RoundedResult, iconPath);
|
return CreateResult(result.RoundedResult, iconPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result CreateResult(decimal result, decimal roundedResult, string iconPath)
|
public static Result CreateResult(decimal roundedResult, string iconPath)
|
||||||
{
|
{
|
||||||
return new Result
|
return new Result
|
||||||
{
|
{
|
||||||
@@ -25,18 +25,19 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
IcoPath = iconPath,
|
IcoPath = iconPath,
|
||||||
Score = 300,
|
Score = 300,
|
||||||
SubTitle = Properties.Resources.wox_plugin_calculator_copy_number_to_clipboard,
|
SubTitle = Properties.Resources.wox_plugin_calculator_copy_number_to_clipboard,
|
||||||
Action = c => Action(result),
|
Action = c => Action(roundedResult),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool Action(decimal result)
|
public static bool Action(decimal roundedResult)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
|
|
||||||
var thread = new Thread(() =>
|
var thread = new Thread(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Clipboard.SetText(result.ToString(CultureInfo.CurrentUICulture.NumberFormat));
|
Clipboard.SetText(roundedResult.ToString(CultureInfo.CurrentUICulture.NumberFormat));
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
catch (ExternalException)
|
catch (ExternalException)
|
||||||
@@ -44,9 +45,11 @@ namespace Microsoft.Plugin.Calculator
|
|||||||
MessageBox.Show(Properties.Resources.wox_plugin_calculator_copy_failed);
|
MessageBox.Show(Properties.Resources.wox_plugin_calculator_copy_failed);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
thread.SetApartmentState(ApartmentState.STA);
|
thread.SetApartmentState(ApartmentState.STA);
|
||||||
thread.Start();
|
thread.Start();
|
||||||
thread.Join();
|
thread.Join();
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user