Skip to content

Commit 1c7a075

Browse files
authored
Merge branch 'main' into gramupdate
2 parents 3342a6a + 2c8a3da commit 1c7a075

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

plugin/ui.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ def query(self, param: str) -> List[dict]:
8888
_("Check documentation for accepted units"),
8989
)
9090
else:
91+
c = do_convert["converted"]
92+
p = plugin.utils.smart_precision(
93+
locale.localeconv()["decimal_point"], c, 3
94+
)
9195
self.sendNormalMess(
9296
(do_convert["category"]),
9397
(
94-
f"{locale.format_string('%.6g', float(args[0]), grouping=True)} {args[1]} = {locale.format_string('%f', do_convert['converted'], grouping=True)} {args[2]}"
98+
f"{locale.format_string('%.10g', float(args[0]), grouping=True)} {args[1]} = {locale.format_string(f'%.{p}f', c, grouping=True)} {args[2]}"
9599
),
96100
f"assets/{do_convert['category']}.ico",
97101
)

plugin/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import plugin.units as gc_units
23
from plugin.extensions import _
34

@@ -109,3 +110,19 @@ def gen_convert(amount: float, from_unit: str, to_unit: str):
109110
"Problem converting {} and {}".format(from_unit, to_unit)
110111
)
111112
return conversions
113+
114+
115+
def smart_precision(separator, amount, preferred=3):
116+
str_amt = str(amount)
117+
dec_places = str_amt[::-1].find(separator)
118+
# whole number
119+
if dec_places == -1:
120+
return 0
121+
frac_part = str_amt[-dec_places::]
122+
# fraction is just zeroes
123+
if int(frac_part) == 0:
124+
return 0
125+
fnz = re.search(r"[1-9]", frac_part).start()
126+
if fnz < preferred:
127+
return preferred
128+
return fnz + 1

0 commit comments

Comments
 (0)