Fix: trailing zeros added to the rounded value with split units

When units were split into larger and smaller values, the larger
value is rounded and should not show trailing zeros.
Trailing zeros should only be used for the smaller unit.

Ref !140790
This commit is contained in:
Campbell Barton
2025-09-25 13:55:21 +10:00
parent a69dede3f1
commit 84559f8bd4
2 changed files with 12 additions and 1 deletions

View File

@@ -1755,7 +1755,8 @@ static size_t unit_as_string_split_pair(char *str,
/* Check the 2 is a smaller unit. */
if (unit_b > unit_a) {
size_t i = unit_as_string(str, str_maxncpy, value_a, prec, do_rstrip_zero, usys, unit_a, '\0');
/* Always strip zeros for the larger unit, since it is truncated and won't ever "jitter". */
size_t i = unit_as_string(str, str_maxncpy, value_a, prec, true, usys, unit_a, '\0');
prec -= integer_digits_d(value_a / unit_b->scalar) -
integer_digits_d(value_b / unit_b->scalar);

View File

@@ -60,15 +60,25 @@ class UnitsTesting(unittest.TestCase):
# LENGTH
# Note: precision handling is a bit complicated when using multi-units...
('IMPERIAL', 'LENGTH', 3, False, False, 0.3048, "1'"),
('IMPERIAL', 'LENGTH', -3, False, False, 0.3048, "1.000'"),
('IMPERIAL', 'LENGTH', 3, False, True, 0.3048, "1ft"),
('IMPERIAL', 'LENGTH', -3, False, True, 0.3048, "1.000ft"),
('IMPERIAL', 'LENGTH', -6, False, True, 0.3048, "1.000000ft"),
('IMPERIAL', 'LENGTH', -7, False, True, 0.3048, "1.000000ft"),
('IMPERIAL', 'LENGTH', 4, True, False, 0.3048 * 2 + 0.0254 * 5.5, "2' 5.5\""),
('IMPERIAL', 'LENGTH', -4, True, False, 0.3048 * 2 + 0.0254 * 5.5, "2' 5.50\""),
('IMPERIAL', 'LENGTH', 3, False, False, 1609.344 * 1e6, "1000000 mi"),
('IMPERIAL', 'LENGTH', 6, False, False, 1609.344 * 1e6, "1000000 mi"),
('METRIC', 'LENGTH', 3, True, False, 1000 * 2 + 0.001 * 15, "2 km 2 cm"),
('METRIC', 'LENGTH', 3, True, False, 0.000005, "5 µm"),
('METRIC', 'LENGTH', -3, True, False, 0.000005, "5.00 µm"),
('METRIC', 'LENGTH', 5, True, False, 1234.56789, "1 km 234.6 m"),
('METRIC', 'LENGTH', 6, True, False, 1234.56789, "1 km 234.57 m"),
('METRIC', 'LENGTH', 9, False, False, 1234.56789, "1.234568 km"),
('METRIC', 'LENGTH', 9, True, False, 1000.000123456789, "1 km 0.123 mm"),
('METRIC', 'LENGTH', 7, True, False, 0, "0 m"),
('METRIC', 'LENGTH', -5, True, False, 0, "0.00000 m"),
('METRIC', 'LENGTH', -7, True, False, 0, "0.000000 m"),
)
def test_units_inputs(self):