Skip to content

Fix RIGHT() for num_chars between 0 and 1 - #184

Merged
dgorissen merged 1 commit into
dgorissen:masterfrom
zzoraemon043:fix/right-fractional-num-chars
Nov 28, 2026
Merged

dgorissen merged 1 commit into
dgorissen:masterfrom
zzoraemon043:fix/right-fractional-num-chars

Conversation

@zzoraemon043

Copy link
Copy Markdown
Contributor

RIGHT() checks the raw num_chars against 0 but slices with -int(num_chars), so any value between 0 and 1 becomes str[-0:] and returns the whole text.

Formula Excel pycel master
=RIGHT("abcd",0.5) "" abcd
=RIGHT("abcd","0.5") "" abcd
=RIGHT(123,0.5) "" 123
=RIGHT("abcd",-0.5) #VALUE! #VALUE!

Excel truncates num_chars after the negative check, the same way LEFT() here already does. The fix truncates before the zero check.

  • tests added / passed: new test_right cases fail on master and pass with the fix; full pytest tests passes (3100), flake8 on the changed files shows no new warnings
  • Checked against Excel 16: 2000 random RIGHT() calls (fractional, negative, text and boolean num_chars) differ from Excel in 520 cases on master and in none with the fix.

Not changed: LEFT/RIGHT/MID/REPLACE still return #VALUE! for text counts such as "10%" or "1,000", which Excel converts to numbers.

RIGHT() compared the raw num_chars to 0 but sliced with -int(num_chars),
so 0 < num_chars < 1 gave str[-0:], the whole text. Excel truncates
num_chars first: =RIGHT("abcd",0.5) is an empty string.
@dgorissen
dgorissen merged commit 95ee55a into dgorissen:master Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants