Skip to content

Commit a0ae760

Browse files
Fix precision bug
1 parent 5e37c82 commit a0ae760

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

engineering_notation/eng_notation.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class EngUnit:
9797
def __init__(
9898
self,
9999
value: str | int | float | EngNumber | EngUnit,
100-
precision: int = 2,
100+
precision: int | None = None,
101101
significant: int = 0,
102102
unit: str | None = None,
103103
separator: str = "",
@@ -108,6 +108,7 @@ def __init__(
108108
Args:
109109
value: String, int, float, or EngNumber value to parse.
110110
precision: Decimal places used when significant is 0.
111+
Defaults to 2 when None.
111112
significant: Significant digits; takes precedence over precision.
112113
unit: Explicit unit suffix. If None, a unit suffix is parsed from
113114
the end of a string value when present.
@@ -448,7 +449,7 @@ class EngNumber:
448449
def __init__(
449450
self,
450451
value: str | int | float | EngNumber,
451-
precision: int = 2,
452+
precision: int | None = None,
452453
significant: int = 0,
453454
separator: str = "",
454455
) -> None:
@@ -458,14 +459,16 @@ def __init__(
458459
Args:
459460
value: String, int, float, or EngNumber value to parse.
460461
precision: Decimal places used when significant is 0.
462+
Defaults to 2 when None.
461463
significant: Significant digits; takes precedence over precision.
462464
separator: String inserted between the numeric value and suffix.
463465
464466
Raises:
465467
TypeError: If the value type is unsupported.
466468
ValueError: If a string value cannot be parsed by Decimal.
467469
"""
468-
self.precision = precision
470+
self.precision = 2 if precision is None else precision
471+
self._precision_explicit = precision is not None
469472
self.significant = significant
470473
self.separator = separator
471474

@@ -560,7 +563,11 @@ def __repr__(self) -> str:
560563
base = base.rstrip(".")
561564

562565
# remove trailing .00 in precision 2
563-
if self.precision == 2 and self.significant == 0:
566+
if (
567+
self.precision == 2
568+
and self.significant == 0
569+
and not self._precision_explicit
570+
):
564571
if ".00" in base:
565572
base = base[:-3]
566573

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "engineering-notation"
3-
version = "0.13.1"
3+
version = "0.13.2"
44
description = "Easy engineering notation"
55
readme = "readme.md"
66
requires-python = ">=3.12"

tests/test_engnum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ def test_engnum_significant():
7171
assert str(EngNumber("2.22m", significant=3)) == "2.22m"
7272

7373

74+
def test_engnum_precision_rounding():
75+
assert str(EngNumber("1.234k", precision=1)) == "1.2k"
76+
assert str(EngNumber("1.234k", precision=3)) == "1.234k"
77+
assert str(EngNumber("1.2k", precision=0)) == "1k"
78+
assert str(EngNumber("-1.234k", precision=1)) == "-1.2k"
79+
assert str(EngNumber("1.666666", precision=2)) == "1.67"
80+
81+
82+
def test_engnum_precision_trailing_zeros():
83+
assert str(EngNumber("1k", precision=2)) == "1.00k"
84+
assert str(EngNumber("1k", precision=3)) == "1.000k"
85+
assert str(EngNumber("2.0", precision=2)) == "2.00"
86+
87+
7488
def test_engnum_separator():
7589
assert str(EngNumber("1.23k", separator=" ")) == "1.23 k"
7690
assert str(EngNumber("1.23k", separator=" ").to_pn()) == "1k23"
@@ -366,6 +380,12 @@ def test_to_str():
366380
assert EngUnit("2m", unit="meter").eng_num == EngNumber("2m")
367381

368382

383+
def test_engunit_precision():
384+
assert str(EngUnit("1.234kHz", precision=1)) == "1.2kHz"
385+
assert str(EngUnit("1kHz", precision=3)) == "1.000kHz"
386+
assert str(EngUnit("-1.234kHz", precision=1)) == "-1.2kHz"
387+
388+
369389
def test_to_str_large():
370390
# positive_numbers
371391
assert str(EngUnit("220kHz")) == "220kHz"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)