Skip to content

Stop printing two minus signs in fractional for a negative mixed number#320

Merged
hugovk merged 1 commit into
python-humanize:mainfrom
c-tonneslan:fix/fractional-negative-double-sign
Jun 25, 2026
Merged

Stop printing two minus signs in fractional for a negative mixed number#320
hugovk merged 1 commit into
python-humanize:mainfrom
c-tonneslan:fix/fractional-negative-double-sign

Conversation

@c-tonneslan

Copy link
Copy Markdown
Contributor

`fractional(-1.3)` currently returns `'-1 -3/10'`. Same shape for any negative number whose absolute value is at least 1: `fractional(-2.5)` returns `'-2 -1/2'`, etc.

`int()` truncates toward zero, so `int(-1.3)` is `-1` and `-1.3 - (-1)` is `-0.3`, which `Fraction` keeps as `-3/10`. Both the whole-number part and the numerator end up signed and the format string prints them both verbatim.

The minus sign already rides on the whole-number part, so absorb it from the numerator with `abs()`. The result is the conventional mixed-fraction form `'-1 3/10'` (= -1.3).

Added negative-input cases to `test_fractional`.

int() truncates toward zero, so int(-1.3) is -1 and (-1.3) - (-1) is
-0.3, which Fraction turns into -3/10. Both the whole-number part and
the numerator end up signed and the output reads as "-1 -3/10".
The minus sign already rides on the whole-number part; absorb it
from the numerator so the result is the conventional "-1 3/10".

Added a few negative cases to test_fractional.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@codecov

codecov Bot commented May 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.55%. Comparing base (013969a) to head (331b4c6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #320   +/-   ##
=======================================
  Coverage   99.55%   99.55%           
=======================================
  Files          12       12           
  Lines         898      900    +2     
=======================================
+ Hits          894      896    +2     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.55% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 97.55% <100.00%> (+<0.01%) ⬆️
windows-latest 95.66% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented May 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks


Comparing c-tonneslan:fix/fractional-negative-double-sign (331b4c6) with main (0a06a3d)

Open in CodSpeed

@hugovk hugovk added the changelog: Fixed For any bug fixes label May 24, 2026
@hugovk hugovk merged commit c2c410c into python-humanize:main Jun 25, 2026
46 of 47 checks passed
@hugovk

hugovk commented Jun 25, 2026

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For any bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants