Fix: Trim leading zeros and keep decimal digits in formatted fields#7809
Fix: Trim leading zeros and keep decimal digits in formatted fields#7809CarolineDenis wants to merge 5 commits intomainfrom
Conversation
alesan99
left a comment
There was a problem hiding this comment.
- Verify that the leading 0s are removed for CO and that the decimals after the longitude and latitude are kept.
The formatting is working as expected 👍

The frontend formatter remains unchanged, but I think that's fine since it trims the trailing zeros anyway. Just making a note.
Though since the setting isn't just used to remove leading zeros anymore, could it be renamed to "Trim zeros"?
Triggered by e0cd4a4 on branch refs/heads/issue-7451
acwhite211
left a comment
There was a problem hiding this comment.
Looks good, might need a small change for handling different formats.
| def apply_stringish(expr): | ||
| e = expr | ||
|
|
||
| if fieldNodeAttrib.get('trimzeros') == 'true': | ||
| numeric_str = cast(cast(e, types.Numeric(65)), types.String()) | ||
| e = case( | ||
| (e.op('REGEXP')(r'^-?[0-9]+(\.[0-9]+)?$'), numeric_str), | ||
| else_=cast(e, types.String()), | ||
| ) | ||
| e = cast(e, types.String()) | ||
|
|
||
| # remove leading zeros | ||
| e = func.regexp_replace(e, r'^(-?)0+([0-9])', r'\1\2') | ||
|
|
||
| # remove trailing zeros after decimal | ||
| e = func.regexp_replace(e, r'(\.[0-9]*?)0+$', r'\1') | ||
|
|
||
| # remove trailing decimal point | ||
| e = func.regexp_replace(e, r'\.$', '') | ||
|
|
||
| fmt = fieldNodeAttrib.get('format') | ||
| if fmt is not None: | ||
| e = self.pseudo_sprintf(fmt, e) | ||
|
|
||
| sep = fieldNodeAttrib.get('sep') | ||
| if sep is not None: | ||
| e = concat(sep, e) | ||
|
|
||
| return e |
There was a problem hiding this comment.
I think this needs a small change in the trimzeros logic for handling different formatting.
Looks like the decimal loss case for numeric values is fixed, but it also removes the old guard that only normalized values when the entire value was numeric. As a result, values that only look numeric at the start or end can now be rewritten unexpectedly.
We can keep this fix, but only apply it when the full value matches the numeric pattern:
def apply_stringish(expr):
e = expr
if fieldNodeAttrib.get('trimzeros') == 'true':
e = cast(e, types.String())
trimmed = e
# remove leading zeros
trimmed = func.regexp_replace(trimmed, r'^(-?)0+([0-9])', r'\1\2')
# remove trailing zeros after decimal
trimmed = func.regexp_replace(trimmed, r'(\.[0-9]*?)0+$', r'\1')
# remove trailing decimal point
trimmed = func.regexp_replace(trimmed, r'\.$', '')
e = case((e.op('REGEXP')(r'^-?[0-9]+(\.[0-9]+)?$'), trimmed), else_=e)
fmt = fieldNodeAttrib.get('format')
if fmt is not None:
e = self.pseudo_sprintf(fmt, e)
sep = fieldNodeAttrib.get('sep')
if sep is not None:
e = concat(sep, e)
return eExamples cases:
| Input | Current PR result | New result |
|---|---|---|
0001-A |
1-A |
0001-A |
ABC.2300 |
ABC.23 |
ABC.2300 |
INV0007.000 |
INV0007 |
INV0007.000 |
Numeric inputs would continue to work as intended:
| Input | Current PR result | New result |
|---|---|---|
001.230400 |
1.2304 |
1.2304 |
0000 |
0 |
0 |
Let me know if that would be a good change or not?

Fixes #7451
Checklist
self-explanatory (or properly documented)
Testing instructions