Skip to content

Commit

Permalink
Round before reformatting percentage input
Browse files Browse the repository at this point in the history
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%
  • Loading branch information
VirginiaDooley committed Jul 18, 2024
1 parent a6edc5a commit 925ec98
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ynr/apps/utils/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 925ec98

Please sign in to comment.