diff --git a/Flow.Launcher.Test/Plugins/CalculatorTest.cs b/Flow.Launcher.Test/Plugins/CalculatorTest.cs index 4e40d3645b0..58217d9320d 100644 --- a/Flow.Launcher.Test/Plugins/CalculatorTest.cs +++ b/Flow.Launcher.Test/Plugins/CalculatorTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Reflection; using Flow.Launcher.Plugin.Calculator; @@ -114,6 +114,10 @@ public void ThousandsSeparatorTest_LargeNumber() [TestCase(@"sqrt(-1)", "")] [TestCase(@"log(0)", "")] [TestCase(@"invalid_expression", "")] + // Floating point precision + [TestCase(@"3000000-2911111.82", "88888.18")] + [TestCase(@"123456789012345", "123456789012345")] + [TestCase(@"9007199254740991", "9007199254740991")] public void CalculatorTest(string expression, string result) { _settings.UseThousandsSeparator = false; diff --git a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs index 1b9a38e1819..62e12f9b6cd 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -128,7 +128,18 @@ public List Query(Query query) if (!string.IsNullOrEmpty(result.ToString())) { - decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero); + double rawValue = Convert.ToDouble(result); + decimal decValue; + if (double.IsNaN(rawValue) || double.IsInfinity(rawValue)) + { + decValue = (decimal)rawValue; + } + else + { + string format = (rawValue % 1 == 0) ? "G17" : "G14"; + decValue = decimal.Parse(rawValue.ToString(format, CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture); + } + decimal roundedResult = Math.Round(decValue, _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero); string newResult = FormatResult(roundedResult); return @@ -138,7 +149,6 @@ public List Query(Query query) Title = newResult, IcoPath = IcoPath, Score = 300, - // Check context nullability for unit testing SubTitle = Context == null ? string.Empty : Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(), CopyText = newResult, Action = c =>