Skip to content

Commit

Permalink
Format floats using the intrinsic decimal precision instead of reduci…
Browse files Browse the repository at this point in the history
…ng to 5 decimal places

Explicitly format floats in outline color test so they can be compared
  • Loading branch information
programmarchy committed Aug 25, 2022
1 parent 2ddc48a commit e73cb59
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PyPDF2/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __repr__(self) -> str:
return str(self.quantize(decimal.Decimal(1)))
else:
# Standard formatting adds useless extraneous zeros.
o = f"{self:.5f}"
o = '{0:f}'.format(self)
# Remove the zeros.
while o and o[-1] == "0":
o = o[:-1]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import decimal
import os
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -777,7 +778,7 @@ def test_colors_in_outline_item():
reader2 = PdfReader(target)
for outline_item in reader2.outline:
# convert float to string because of mutability
assert [str(c) for c in outline_item.color] == [str(p) for p in purple_rgb]
assert [f"{c:.5f}" for c in outline_item.color] == [f"{p:.5f}" for p in purple_rgb]

# Cleanup
os.remove(target) # remove for testing
Expand Down

0 comments on commit e73cb59

Please sign in to comment.