@@ -56,10 +56,8 @@ def _format_not_finite(value: float) -> str:
5656
5757 if math .isnan (value ):
5858 return "NaN"
59- if math .isinf (value ) and value < 0 :
60- return "-Inf"
61- if math .isinf (value ) and value > 0 :
62- return "+Inf"
59+ if math .isinf (value ):
60+ return "+Inf" if value > 0 else "-Inf"
6361 return ""
6462
6563
@@ -156,21 +154,15 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
156154 value = value .replace (thousands_sep , "" ).replace (decimal_sep , "." )
157155 if not math .isfinite (float (value )):
158156 return _format_not_finite (float (value ))
159- if "." in value :
160- value = float (value )
161- else :
162- value = int (value )
157+ value = float (value ) if "." in value else int (value )
163158 else :
164159 if not math .isfinite (float (value )):
165160 return _format_not_finite (float (value ))
166161 float (value )
167162 except (TypeError , ValueError ):
168163 return str (value )
169164
170- if ndigits is not None :
171- result = f"{ value :,.{ndigits }f} "
172- else :
173- result = f"{ value :,} "
165+ result = f"{ value :,.{ndigits }f} " if ndigits is not None else f"{ value :,} "
174166 if thousands_sep != "," or decimal_sep != "." :
175167 result = result .translate (str .maketrans (",." , thousands_sep + decimal_sep ))
176168 return result
@@ -547,12 +539,12 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
547539
548540 old_bucket = exponent // 3 * 3
549541 value /= 10 ** old_bucket
550- digits = int ( max (0 , precision - exponent % 3 - 1 ) )
542+ digits = max (0 , precision - exponent % 3 - 1 )
551543 if exponent < 30 and round (abs (value ), digits ) >= 1000 :
552544 exponent += 3 - exponent % 3
553545 new_bucket = exponent // 3 * 3
554546 value /= 10 ** (new_bucket - old_bucket )
555- digits = int ( max (0 , precision - exponent % 3 - 1 ) )
547+ digits = max (0 , precision - exponent % 3 - 1 )
556548
557549 if exponent >= 3 :
558550 ordinal_ = "kMGTPEZYRQ" [exponent // 3 - 1 ]
@@ -561,9 +553,6 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
561553 else :
562554 ordinal_ = ""
563555 value_ = format (value , f".{ digits } f" )
564- if not (unit or ordinal_ ) or unit in ("°" , "′" , "″" ):
565- space = ""
566- else :
567- space = " "
556+ space = "" if not (unit or ordinal_ ) or unit in ("°" , "′" , "″" ) else " "
568557
569558 return f"{ value_ } { space } { ordinal_ } { unit } "
0 commit comments