Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: read non-ASCII digits in NumericComparator
NumericComparator kept only [0-9.-] before parsing, so decimal digits
outside ASCII (Arabic-Indic, Persian, Devanagari, Thai, fullwidth, ...)
were deleted rather than read. "١٢٥٠" never matched "1250", and
"١٢٥٠.50" read as 0.50, which could match the wrong number.

A string whose digits are all outside ASCII read as no number before,
so the new reader returns a number only when the string holds one
unambiguous number, and no number otherwise. It understands the Arabic
separators, Indian grouping, and the currency, accounting and minus
formats CLDR writes; separators are accepted only for scripts whose
locales all use "." as the decimal point, and a word counts as a
currency only when it is a CLDR currency joined to the number as CLDR
joins it.

A string with ASCII digits reads as before, except that a number whose
digits switch script reads as no number, and that Unicode minus signs,
fullwidth accounting parentheses and spaces around accounting
parentheses now mark a negative the old reading missed, and a negative
inside accounting parentheses ("(-1250)") is no longer negated twice.

Fixes #559
  • Loading branch information
nursnaaz committed Nov 17, 2026
commit c8042535edfd74f8f36b99c2c52129a31d00787c
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ Each release links to full notes on the

- `NumericComparator` treats booleans as non-numeric and returns `0.0` instead of
raising `decimal.InvalidOperation`, including in uncoerced model fields.
- **`NumericComparator` reads digits in other scripts.** Digits outside ASCII
(Arabic-Indic, Persian, Devanagari, Thai, fullwidth, ...) were deleted before
parsing, so `"١٢٥٠"` never matched `"1250"` and `"١٢٥٠.50"` read as `0.50`. A
string written in such digits now reads as its number when it holds one
unambiguous number, and otherwise as no number, as before. A number whose
digits switch script reads as no number, unless it is the same amount
written twice (`"1250١٢٥٠"`). A compact form such as `"١٫٣ ألف"` reads the
number shown, as `"1.3K"` does. String values only: a `float`, `int` or
`Decimal` field rejects `"١٬٢٥٠٫٥٠"` before the comparator runs
([#559](https://github.com/awslabs/stickler/issues/559))
- `NumericComparator` reads as negative some ASCII-digit values that it read
as positive: U+2212 and other minus signs that open the value, before the
number, a currency or a parenthesis (`"−1250"`, `"−AED 1250"`,
`"−(1250)"`, `"−.75"`), or come right after a currency symbol (`"$−1250"`);
fullwidth accounting parentheses (`"(1250)"`); accounting parentheses with
spaces or invisible format characters around them (`" (1250) "`); and a
negative inside accounting parentheses, which was negated twice
(`"(-1250)"`). A minus sign after a word keeps its old reading
(`"Total – 1250"` reads 1250)
([#559](https://github.com/awslabs/stickler/issues/559))

### Security

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/Guides/Comparators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class Contact(StructuredModel):

Extracts numeric values from strings or numbers and compares them with configurable tolerance. Handles currency symbols, commas, and accounting notation (e.g., `(123)` for negative values). Returns 1.0 if the numbers match within tolerance, 0.0 otherwise.

String values may use decimal digits in other scripts: `"١٬٢٥٠٫٥٠"` reads 1250.50, as `"1,250.50"` does. A value whose digits are all outside ASCII reads as no number unless it holds one unambiguous number. A value that also has ASCII digits reads its ASCII number and ignores its other-script digits (`"1,250.00 (١٬٢٥٠٫٠٠)"` reads 1250.00), but a number whose digits switch script, such as `"١٢٥٠.50"`, reads as no number unless it is the same amount written twice. A minus sign such as U+2212 marks a negative when it opens the value or follows a currency symbol (`"−1250"`, `"$−1250"`), as do accounting parentheses that are fullwidth or have spaces around them (`"(1250)"`, `" (1250) "`); `"(-1250)"` reads -1250. A compact form such as `"١٫٣ ألف"` reads the number shown (1.3), as `"1.3K"` does; one with a CJK numeral (`"12万円"`) reads as no number. Declare the field `str`: a `float`, `int` or `Decimal` field rejects `"١٬٢٥٠٫٥٠"` before the comparator runs.

**When to use:** Prices, quantities, measurements, or any numeric field where small differences are acceptable.

```python
Expand Down
1 change: 1 addition & 0 deletions src/stickler/comparators/Comparators.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ comparator = NumericComparator()
- Specialized for comparing numeric values
- Configurable absolute and relative tolerance for floating-point comparisons
- Handles type conversion from strings
- Reads decimal digits in other scripts in string values (`١٢٥٠` equals `1250`)
- Ideal for financial data or measurement comparisons

#### Date Comparator
Expand Down
Loading
Loading