Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Format floats using their intrinsic decimal precision #1267

Merged
merged 3 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion PyPDF2/_writer.py
Expand Up @@ -1921,8 +1921,11 @@ def _create_outline_item(
if color:
if isinstance(color, str):
color = hex_to_rgb(color)
prec = decimal.Decimal('1.00000')
outline_item.update(
{NameObject("/C"): ArrayObject([FloatObject(c) for c in color])}
{NameObject("/C"): ArrayObject([
FloatObject(decimal.Decimal(c).quantize(prec)) for c in color
])}
)
if italic or bold:
format_flag = 0
Expand Down
10 changes: 4 additions & 6 deletions PyPDF2/generic/_base.py
Expand Up @@ -241,14 +241,12 @@ def __new__(

def __repr__(self) -> str:
if self == self.to_integral():
# If this is an integer, format it with no decimal place.
return str(self.quantize(decimal.Decimal(1)))
else:
# Standard formatting adds useless extraneous zeros.
o = f"{self:.5f}"
# Remove the zeros.
while o and o[-1] == "0":
o = o[:-1]
return o
# Otherwise, format it with a decimal place, taking care to
# remove any extraneous trailing zeros.
return f"{self:f}".rstrip("0")

def as_numeric(self) -> float:
return float(repr(self).encode("utf8"))
Expand Down
32 changes: 32 additions & 0 deletions tests/test_generic.py
Expand Up @@ -862,3 +862,35 @@ def test_create_string_object_force():
assert create_string_object(b"Hello World", []) == "Hello World"
assert create_string_object(b"Hello World", {72: "A"}) == "Aello World"
assert create_string_object(b"Hello World", "utf8") == "Hello World"


@pytest.mark.parametrize(
("value", "expected"),
[
("0.000000", "0"),
("0.0", "0"),
("1.0", "1"),
("0.123000", "0.123"),
("0.000123000", "0.000123"),
("0.0", "0"),
("0", "0"),
("1", "1"),
("1.0", "1"),
("1.01", "1.01"),
("1.010", "1.01"),
("0000.0000", "0"),
("0.10101010", "0.1010101"),
("50000000000", "50000000000"),
("99900000000000000123", "99900000000000000123"),
("99900000000000000123.456000", "99900000000000000123.456"),
("0.00000000000000000000123", "0.00000000000000000000123"),
("0.00000000000000000000123000", "0.00000000000000000000123"),
("50032481330523882508234.00000000000000000000123000", "50032481330523882508234.00000000000000000000123"),
(
"928457298572093487502198745102973402987412908743.75249875981374981237498213740000",
"928457298572093487502198745102973402987412908743.7524987598137498123749821374"
),
]
)
def test_float_object_decimal_to_string(value, expected):
assert repr(FloatObject(value)) == expected