From 84559f8bd46149382b893d35ef67af4e6beabf63 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 25 Sep 2025 13:55:21 +1000 Subject: [PATCH] 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 --- source/blender/blenkernel/intern/unit.cc | 3 ++- tests/python/bl_pyapi_bpy_utils_units.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/source/blender/blenkernel/intern/unit.cc b/source/blender/blenkernel/intern/unit.cc index 1892bf40d20..e0c8e7965d1 100644 --- a/source/blender/blenkernel/intern/unit.cc +++ b/source/blender/blenkernel/intern/unit.cc @@ -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); diff --git a/tests/python/bl_pyapi_bpy_utils_units.py b/tests/python/bl_pyapi_bpy_utils_units.py index df3d96806e8..a27ea1cf7ac 100644 --- a/tests/python/bl_pyapi_bpy_utils_units.py +++ b/tests/python/bl_pyapi_bpy_utils_units.py @@ -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):