Skip to content

fix(Helpers): prevent trailing comma in format_number() with zero precision - #10793

Merged
paulbalandan merged 2 commits into
codeigniter4:developfrom
banghamid:fix/number-helper-format-locale-precision-zero
Nov 20, 2026
Merged

paulbalandan merged 2 commits into
codeigniter4:developfrom
banghamid:fix/number-helper-format-locale-precision-zero

Conversation

@banghamid

@banghamid banghamid commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description
In number_helper.php, format_number() constructed the NumberFormatter pattern as:

$pattern = '#,##0.' . str_repeat('#', $precision);

When $precision === 0, this resulted in '#,##0.'. The trailing decimal symbol forced NumberFormatter::format() to always append the locale's localized decimal separator.

While trailing periods were trimmed with trim($output, '. '), locales that use a comma , as the decimal separator (such as de, fr, es, it, id, nl, pt, ru, etc.) retained the trailing comma:

  • format_number(123456, 0, 'de_DE') produced '123.456,' instead of '123.456'
  • number_to_amount(123000, 0, 'de_DE') produced '123, thousand' instead of '123 thousand'
  • number_to_size(1024, 0, 'de_DE') produced '1, KB' instead of '1 KB'

This PR only includes the decimal separator in the pattern when $precision > 0:

$pattern = '#,##0' . ($precision > 0 ? '.' . str_repeat('#', $precision) : '');

It also adds test assertions in NumberHelperTest for format_number(), number_to_size(), and number_to_amount() using comma-decimal locales, and adds a changelog entry in docs/changelogs/v4.7.7.md.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 19, 2026
Comment thread system/Helpers/number_helper.php Resolved

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@paulbalandan
paulbalandan merged commit e4d791d into codeigniter4:develop Nov 20, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants