Added allow_negative to some formatting functions

pull/273/head
Holger Müller 2020-07-19 16:37:18 +02:00
rodzic 6052b09687
commit 0c59600f4a
1 zmienionych plików z 10 dodań i 6 usunięć

Wyświetl plik

@ -35,6 +35,7 @@ FMT_GROUP_DELAY = SITools.Format(max_nr_digits=5, space_str=" ")
FMT_REACT = SITools.Format(max_nr_digits=5, space_str=" ", allow_strip=True)
FMT_COMPLEX = SITools.Format(max_nr_digits=3, allow_strip=True,
printable_min=0, unprintable_under="- ")
FMT_COMPLEX_NEG = SITools.Format(max_nr_digits=3, allow_strip=True)
FMT_PARSE = SITools.Format(parse_sloppy_unit=True, parse_sloppy_kilo=True,
parse_clamp_min=0)
@ -65,8 +66,8 @@ def format_gain(val: float, invert: bool = False) -> str:
return f"{val:.3f} dB"
def format_q_factor(val: float) -> str:
if val < 0 or val > 10000.0:
def format_q_factor(val: float, allow_negative: bool = False) -> str:
if (not allow_negative and val < 0) or abs(val > 10000.0):
return "\N{INFINITY}"
return str(SITools.Value(val, fmt=FMT_Q_FACTOR))
@ -79,8 +80,8 @@ def format_magnitude(val: float) -> str:
return f"{val:.3f}"
def format_resistance(val: float) -> str:
if val < 0:
def format_resistance(val: float, allow_negative: bool = False) -> str:
if not allow_negative and val < 0:
return "- \N{OHM SIGN}"
return str(SITools.Value(val, "\N{OHM SIGN}", FMT_REACT))
@ -105,8 +106,11 @@ def format_phase(val: float) -> str:
return f"{math.degrees(val):.2f}\N{DEGREE SIGN}"
def format_complex_imp(z: complex) -> str:
re = SITools.Value(z.real, fmt=FMT_COMPLEX)
def format_complex_imp(z: complex, allow_negative: bool = False) -> str:
fmt_re = FMT_COMPLEX
if allow_negative:
fmt_re = FMT_COMPLEX_NEG
re = SITools.Value(z.real, fmt=fmt_re)
im = SITools.Value(abs(z.imag), fmt=FMT_COMPLEX)
return f"{re}{'-' if z.imag < 0 else '+'}j{im} \N{OHM SIGN}"