Refactoring of formatting for 0 in tablegenerator
With the switch to Decimal, we can use more of the same code that also
handles other numbers for handling 0.
@@ -382,16 +382,15 @@ def _format_number(
"""
assert format_target in POSSIBLE_FORMAT_TARGETS, "Invalid format " + format_target
- # Round to the given amount of significant digits
- intended_digits = min(initial_value_sig_digits, number_of_significant_digits)
if number == 0:
- formatted_value = "0"
- if max_digits_after_decimal > 0 and initial_value_sig_digits > 0:
- formatted_value += "." + "0" * min(
- max_digits_after_decimal, initial_value_sig_digits
- )
+ intended_digits = min(max_digits_after_decimal, initial_value_sig_digits)
+ # Add as many trailing zeros as desired
+ rounded_value = Decimal(0).scaleb(-intended_digits)
else:
+ # Round to the given amount of significant digits
+ intended_digits = min(initial_value_sig_digits, number_of_significant_digits)
+
assert number.adjusted() == int(floor(log10(abs(number))))
rounding_point = -number.adjusted() + (intended_digits - 1)
# Contrary to its documentation, round() seems to be affected by the rounding
