From 22e2e3586865694ff166b9d52625a41f3a4f8f92 Mon Sep 17 00:00:00 2001 From: NPodlozhniy Date: Thu, 2 May 2024 15:31:04 +0300 Subject: [PATCH] Fix formatting for low decimal numbers --- podlozhnyy_module/regression.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/podlozhnyy_module/regression.py b/podlozhnyy_module/regression.py index e7b2277..7261ffb 100644 --- a/podlozhnyy_module/regression.py +++ b/podlozhnyy_module/regression.py @@ -151,7 +151,7 @@ def _format_buck(row): return df.assign(bucket=bucket) -def _format_(x, decimal=3): +def _format_(x, decimal=2): """ Форматируем названия бакетов @@ -169,17 +169,21 @@ def _format_(x, decimal=3): return "%d" % x if div == 0: power = int(np.floor(np.log10(abs(mod)))) - digits = decimal - power - 1 - return "%s" % np.around(x, digits) + if power > -3: + digits = decimal - power - 1 + return "%s" % np.around(x, digits) + else: + return f"%.{decimal}e" % x else: power = int(np.floor(np.log10(abs(div)))) - digits = decimal if power < 3: - return "%s" % np.around(x, digits) - elif power < 10: - return "%se+0%s" % (np.around(x / np.power(10, power), digits), power) + return "%s" % np.around(x, decimal) else: - return "%se+%s" % (np.around(x / np.power(10, power), digits), power) + # "%se+%s" % (np.around(x / np.power(10, power), decimal), power) + # doesn't seem working the same way in case of power is above 9 + # as np.power(10, 10) returns something weird - 1410065408 + # it's casting to float64 to perform the operation and then casting back + return f"%.{decimal}e" % x return "%s" % x