From 1ee38ccee02e8b13fbf0dd110079066d4b98aa48 Mon Sep 17 00:00:00 2001 From: Shinzef Date: Wed, 1 Jul 2026 13:23:37 +0800 Subject: [PATCH 1/2] Fix floating-point precision error in calculator --- Flow.Launcher.Test/Plugins/CalculatorTest.cs | 2 ++ Plugins/Flow.Launcher.Plugin.Calculator/Main.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/CalculatorTest.cs b/Flow.Launcher.Test/Plugins/CalculatorTest.cs index 4e40d3645b0..d499efa7d09 100644 --- a/Flow.Launcher.Test/Plugins/CalculatorTest.cs +++ b/Flow.Launcher.Test/Plugins/CalculatorTest.cs @@ -114,6 +114,8 @@ public void ThousandsSeparatorTest_LargeNumber() [TestCase(@"sqrt(-1)", "")] [TestCase(@"log(0)", "")] [TestCase(@"invalid_expression", "")] + // Floating point precision + [TestCase(@"3000000-2911111.82", "88888.18")] 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..d3cdcb19aa2 100644 --- a/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs @@ -128,7 +128,17 @@ 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; + try + { + decValue = decimal.Parse(rawValue.ToString("G14", CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture); + } + catch (FormatException) + { + decValue = (decimal)rawValue; + } + decimal roundedResult = Math.Round(decValue, _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero); string newResult = FormatResult(roundedResult); return @@ -138,7 +148,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 => From 330b93a4ce7a4179b36f3c317115aba66f25df8d Mon Sep 17 00:00:00 2001 From: Shinzef Date: Wed, 1 Jul 2026 13:52:53 +0800 Subject: [PATCH 2/2] Preserve integer precision in calculator --- Flow.Launcher.Test/Plugins/CalculatorTest.cs | 4 +++- Plugins/Flow.Launcher.Plugin.Calculator/Main.cs | 11 ++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/CalculatorTest.cs b/Flow.Launcher.Test/Plugins/CalculatorTest.cs index d499efa7d09..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; @@ -116,6 +116,8 @@ public void ThousandsSeparatorTest_LargeNumber() [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 d3cdcb19aa2..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; @@ -130,13 +130,14 @@ public List Query(Query query) { double rawValue = Convert.ToDouble(result); decimal decValue; - try + if (double.IsNaN(rawValue) || double.IsInfinity(rawValue)) { - decValue = decimal.Parse(rawValue.ToString("G14", CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture); + decValue = (decimal)rawValue; } - catch (FormatException) + else { - decValue = (decimal)rawValue; + 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);