From 676b16bc2985e9bd376fb0b62564c30a05a91c2b Mon Sep 17 00:00:00 2001 From: Ryan Schultz Date: Mon, 28 Aug 2023 10:11:24 -0500 Subject: [PATCH] Spreadsheet Import/Export: Fix feet and inch formatting --- .../ifcopenshell/util/unit.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/unit.py b/src/ifcopenshell-python/ifcopenshell/util/unit.py index d9c3c4259b..e3dc8aa6d0 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/unit.py +++ b/src/ifcopenshell-python/ifcopenshell/util/unit.py @@ -574,9 +574,10 @@ def format_length( if imperial_unit == "foot": feet = int(value) inches = (value - feet) * 12 + elif imperial_unit == "inch": - inches = value % 12 - feet = int(round((value - inches) / 12)) + inches = value * 12 + # Round to the nearest 1/N nearest = round(inches * precision) @@ -586,14 +587,22 @@ def format_length( # If fraction is a whole number, format it accordingly if frac.denominator == 1: - if suppress_zero_inches and frac.numerator == 0: - return f"{feet}'" - return f"{feet}' - {frac.numerator}\"" - if frac.numerator > frac.denominator: + if imperial_unit == "inch": + return f"{round(inches)}\"" + if suppress_zero_inches: + if imperial_unit == "foot": + return f"{round(value)}'" + elif not suppress_zero_inches: + if imperial_unit == "foot": + return f"{round(value)}' - 0\"" + if frac.numerator > frac.denominator and not frac.denominator == 0: remainder = frac.numerator % frac.denominator whole = int((frac.numerator - remainder) / frac.denominator) - return f"{feet}' - {whole} {remainder}/{frac.denominator}\"" - return f"{feet}' - {frac.numerator}/{frac.denominator}\"" + if imperial_unit == "foot": + return f"{feet}' - {whole} {remainder}/{frac.denominator}\"" + elif imperial_unit == "inch": + return f"{whole} {remainder}/{frac.denominator}\"" + elif unit_system == "metric": rounded_val = round(value / precision) * precision return f"{rounded_val:.{decimal_places}f}"