Skip to content

Fix: Trim leading zeros and keep decimal digits in formatted fields#7809

Open
CarolineDenis wants to merge 5 commits intomainfrom
issue-7451
Open

Fix: Trim leading zeros and keep decimal digits in formatted fields#7809
CarolineDenis wants to merge 5 commits intomainfrom
issue-7451

Conversation

@CarolineDenis
Copy link
Contributor

@CarolineDenis CarolineDenis commented Mar 12, 2026

Fixes #7451

Checklist

  • Self-review the PR after opening it to make sure the changes look good and
    self-explanatory (or properly documented)
  • Add relevant issue to release milestone

Testing instructions

  • Create a new field formatter for cat num and check 'trim leading zero'
  • Create a new field formatter for locality, add longitude and latitude to the fields, check trim zeros for each.
  • Open the QB with CO as base table
  • Add CO formatted and locality formatted
  • Verify that the leading 0s are removed for CO and that the decimals after the longitude and latitude are kept.

@CarolineDenis CarolineDenis added this to the 7.12.1 milestone Mar 12, 2026
@github-project-automation github-project-automation bot moved this to 📋Back Log in General Tester Board Mar 12, 2026
@CarolineDenis CarolineDenis requested review from a team and alesan99 March 18, 2026 13:13
Copy link
Contributor

@alesan99 alesan99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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 👍
Image

Image

The frontend formatter remains unchanged, but I think that's fine since it trims the trailing zeros anyway. Just making a note.

formatted = Number.isNaN(Number(formatted))

Image

Though since the setting isn't just used to remove leading zeros anymore, could it be renamed to "Trim zeros"?

@Iwantexpresso Iwantexpresso self-requested a review March 18, 2026 16:44
Copy link
Contributor

@Iwantexpresso Iwantexpresso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Verify that the leading 0s are removed for CO and that the decimals after the longitude and latitude are kept.
Image

nice this looks good!

@CarolineDenis CarolineDenis requested a review from alesan99 March 18, 2026 20:00
Copy link
Member

@acwhite211 acwhite211 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, might need a small change for handling different formats.

Comment on lines 219 to 242
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 e

Examples 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?

@github-project-automation github-project-automation bot moved this from 📋Back Log to Dev Attention Needed in General Tester Board Mar 20, 2026
@CarolineDenis CarolineDenis requested review from a team and acwhite211 March 23, 2026 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Dev Attention Needed

Development

Successfully merging this pull request may close these issues.

"Trim Zeros" option turns decimals into integers

4 participants