From c0d033ff843da9c5f3017edb3cef18050fab1c51 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 28 Nov 2021 17:59:59 +0100 Subject: [PATCH] Math: Fix crash in @convert on small/large single-digit numbers str() converts them to the exponent notation, which makes split('.')[1] crash, since there is no decimal part in the exponent notation when there is a single significant digit. --- plugins/Math/plugin.py | 2 +- plugins/Math/test.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/Math/plugin.py b/plugins/Math/plugin.py index 1285a7581..81c2ff3eb 100644 --- a/plugins/Math/plugin.py +++ b/plugins/Math/plugin.py @@ -246,7 +246,7 @@ class Math(callbacks.Plugin): newNum = convertcore.convert(number, unit1, unit2) if isinstance(newNum, float): zeros = 0 - for char in str(newNum).split('.')[1]: + for char in "{:f}".format(newNum).split('.')[1]: if char != '0': break zeros += 1 diff --git a/plugins/Math/test.py b/plugins/Math/test.py index dc7b6387d..d33aff396 100644 --- a/plugins/Math/test.py +++ b/plugins/Math/test.py @@ -166,6 +166,7 @@ class MathTestCase(PluginTestCase): self.assertResponse('convert 1 m to cm', '100') self.assertResponse('convert m to cm', '100') self.assertResponse('convert 3 metres to km', '0.003') + self.assertResponse('convert 1 cm to km', '1e-05') self.assertResponse('convert 32 F to C', '0') self.assertResponse('convert 32 C to F', '89.6') self.assertResponse('convert [calc 2*pi] rad to degree', '360')