Skip to content

Commit

Permalink
enable dec_place rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
jagerber48 committed Mar 9, 2024
1 parent 1b76012 commit 2ff4959
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/sciform/format_utils/rounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ def round_val_unc(
val: Decimal,
unc: Decimal,
ndigits: int | type[AutoDigits],
round_mode: RoundModeEnum,
*,
use_pdg_sig_figs: bool = False,
) -> tuple[Decimal, Decimal, int]:
"""Simultaneously round the value and uncertainty."""
if unc.is_finite() and unc != 0:
round_digit = get_round_dec_place(
unc,
RoundModeEnum.SIG_FIG,
round_mode,
ndigits,
pdg_sig_figs=use_pdg_sig_figs,
)
Expand All @@ -103,7 +104,7 @@ def round_val_unc(
elif val.is_finite():
round_digit = get_round_dec_place(
val,
RoundModeEnum.SIG_FIG,
round_mode,
ndigits,
pdg_sig_figs=False,
)
Expand Down
10 changes: 2 additions & 8 deletions src/sciform/formatting/number_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from dataclasses import replace
from decimal import Decimal
from typing import TYPE_CHECKING, cast
from warnings import warn

from sciform.api.formatted_number import FormattedNumber
from sciform.format_utils.exponents import get_exp_str, get_val_unc_exp
Expand Down Expand Up @@ -190,13 +189,6 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str
)
raise NotImplementedError(msg)

if options.round_mode is RoundModeEnum.DEC_PLACE:
msg = (
"Precision round mode not available for value/uncertainty formatting. "
"Rounding is always applied as significant figures for the uncertainty."
)
warn(msg, stacklevel=2)

unc = abs(unc)
if exp_mode is ExpModeEnum.PERCENT:
val *= 100
Expand All @@ -220,12 +212,14 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str
val,
unc,
options.ndigits,
options.round_mode,
use_pdg_sig_figs=options.pdg_sig_figs,
)
val_rounded, unc_rounded, round_digit = round_val_unc(
val_rounded,
unc_rounded,
options.ndigits,
options.round_mode,
use_pdg_sig_figs=options.pdg_sig_figs,
)

Expand Down
1 change: 1 addition & 0 deletions tests/unit/format_utils/test_rounding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def test_round_val_unc(self):
val,
unc,
ndigits,
RoundModeEnum.SIG_FIG,
use_pdg_sig_figs=use_pdg_sig_figs,
)
with self.subTest(
Expand Down

0 comments on commit 2ff4959

Please sign in to comment.