From 925ec98cf4b040273fa19adf7f821cde46f21191 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Thu, 18 Jul 2024 10:09:50 +0100 Subject: [PATCH] Round before reformatting percentage input This fixes a previous PR that sought to remove decimals and round percentages to the nearest whole number. The order of operations was causing 50.55 to become 5055. This change fixes that bug and handles input such as 50,55 50.55% 50,55% 50% --- ynr/apps/utils/widgets.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ynr/apps/utils/widgets.py b/ynr/apps/utils/widgets.py index 702a55f28..85fccbd87 100644 --- a/ynr/apps/utils/widgets.py +++ b/ynr/apps/utils/widgets.py @@ -58,7 +58,13 @@ def build_attrs(self, base_attrs, extra_attrs=None): { "pattern": r"[0-9\s\.]*", "oninvalid": "this.setCustomValidity('Enter a percentage or a whole number')", - "onchange": "this.value = Math.round(this.value.replace(/\D/g, '')).toString()", + "onchange": """ + let value = this.value.replace(",", "."); + value = value.replace("%", ""); + value = value.trim(); + value = Math.round(parseFloat(value)).toString(); + this.value = value; + """, } ) return attrs