From 5c81ab90600979cfdcb3fb82154ce4a7f87a3797 Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 4 Jan 2022 22:09:44 +1100 Subject: [PATCH 1/2] add smarter way to do float precisions. Fix #8 --- plugin/ui.py | 6 +++++- plugin/utils.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/plugin/ui.py b/plugin/ui.py index 29f2947..287e05d 100644 --- a/plugin/ui.py +++ b/plugin/ui.py @@ -88,10 +88,14 @@ def query(self, param: str) -> List[dict]: _("Check documentation for accepted units"), ) else: + c = do_convert["converted"] + p = plugin.utils.smart_precision( + locale.localeconv()["decimal_point"], c, 3 + ) self.sendNormalMess( (do_convert["category"]), ( - f"{locale.format_string('%.6g', float(args[0]), grouping=True)} {args[1]} = {locale.format_string('%f', do_convert['converted'], grouping=True)} {args[2]}" + f"{locale.format_string('%.10g', float(args[0]), grouping=True)} {args[1]} = {locale.format_string(f'%.{p}f', c, grouping=True)} {args[2]}" ), f"assets/{do_convert['category']}.ico", ) diff --git a/plugin/utils.py b/plugin/utils.py index 9805d27..6f87094 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -1,3 +1,4 @@ +import re import plugin.units as gc_units from plugin.extensions import _ @@ -112,3 +113,19 @@ def gen_convert(amount: float, from_unit: str, to_unit: str): "Problem converting {} and {}".format(from_unit, to_unit) ) return conversions + + +def smart_precision(separator, amount, preferred=3): + str_amt = str(amount) + dec_places = str_amt[::-1].find(separator) + # whole number + if dec_places == -1: + return 0 + frac_part = str_amt[-dec_places::] + # fraction is just zeroes + if int(frac_part) == 0: + return 0 + fnz = re.search(r"[1-9]", frac_part).start() + if fnz < preferred: + return preferred + return fnz + 1 From 2af71b34666349fda830694db25f35fc48e24363 Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 4 Jan 2022 22:15:00 +1100 Subject: [PATCH 2/2] Version bump --- plugin.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.json b/plugin.json index 18cf05a..281c8eb 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name": "General Converter", "Description": "General weights and measures converter", "Author": "deefrawley", - "Version": "1.1.1", + "Version": "1.1.2", "Language": "python", "Website": "https://github.com/deefrawley/Flow.Launcher.Plugin.GenConvert", "IcoPath": "assets/favicon.ico",