mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
Fix #4290. You can now format custom thousands separators and decimal separators in IfcCSV.
This commit is contained in:
@@ -232,5 +232,6 @@ nest formulas, for example ``concat(title("foo"), lower("Bar"))`` will produce
|
||||
"``title({{value}})``", "``title(""foo"")``", "``Foo``", "Titlecases a string."
|
||||
"``concat({{value}}[, {{value2}}]*)``", "``concat(""foo"", ""bar"")``", "``foobar``", "Concatenates two or more strings."
|
||||
"``round({{value}}, {{precision}})``", "``round(3.123, 0.1)``", "``3.1``", "Rounds ``{{value}}`` to the nearest ``{{precision}}``."
|
||||
"``number({{value}}[, {{decimal_separator}}[, {{thousands_separator}}]])``", "``number(1234.56, "","", ""."")``", "123.4,56", "Formats {{value}} with an optional custom {{decimal_separator}} and {{thousands_separator}}. The default separators are ``.`` and ``,``."
|
||||
"``metric_length({{value}}, {{precision}}, {{decimals}})``", "``metric_length(3.123, 0.1, 2)``", "``3.10``", "Rounds ``{{value}}`` to the nearest ``{{precision}}`` then displays using a certain amount of decimal places."
|
||||
"``imperial_length({{value}}, {{precision}}, {{input_unit}}, {{output_unit}})``", "``imperial_length(3.22, 4, ""foot"")``", "``3' - 3 3/4""``", "``The {{value}}`` may be specified either as ``foot`` or ``inch`` depending on ``{{input_unit}}``. The ``{{value}}`` is then rounded to the nearest ``1/{{precision}}`` inch then formatted using fractional feet and inches if ``{{output_unit}}`` is set to ``foot`` or just inches if ``{{output_unit}}`` is set to ``inch``."
|
||||
|
||||
@@ -129,9 +129,10 @@ get_element_grammar = lark.Lark(
|
||||
format_grammar = lark.Lark(
|
||||
"""start: function
|
||||
|
||||
function: round | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER
|
||||
function: round | number | format_length | lower | upper | title | concat | substr | ESCAPED_STRING | NUMBER
|
||||
|
||||
round: "round(" function "," NUMBER ")"
|
||||
number: "number(" function ["," ESCAPED_STRING ["," ESCAPED_STRING]] ")"
|
||||
format_length: metric_length | imperial_length
|
||||
metric_length: "metric_length(" function "," NUMBER "," NUMBER ")"
|
||||
imperial_length: "imperial_length(" function "," NUMBER ["," ESCAPED_STRING "," ESCAPED_STRING] ")"
|
||||
@@ -212,6 +213,15 @@ class FormatTransformer(lark.Transformer):
|
||||
return str(int(result))
|
||||
return str(result)
|
||||
|
||||
def number(self, args):
|
||||
if isinstance(args[0], str):
|
||||
args[0] = float(args[0]) if "." in args[0] else int(args[0])
|
||||
if len(args) >= 3 and args[2]:
|
||||
return "{:,}".format(args[0]).replace(".", "*").replace(",", args[2]).replace("*", args[1])
|
||||
elif len(args) >= 2 and args[1]:
|
||||
return "{}".format(args[0]).replace(".", args[1])
|
||||
return "{:,}".format(args[0])
|
||||
|
||||
def format_length(self, args):
|
||||
return args[0]
|
||||
|
||||
|
||||
@@ -38,8 +38,8 @@ class TestEditWorkTime(test.bootstrap.IFC4):
|
||||
assert work_time.DataOrigin == attributes["DataOrigin"]
|
||||
assert work_time.UserDefinedDataOrigin == attributes["UserDefinedDataOrigin"]
|
||||
assert work_time.RecurrencePattern == attributes["RecurrencePattern"]
|
||||
assert work_time.Start == "2020-01-01"
|
||||
assert work_time.Finish == "2020-02-01"
|
||||
assert work_time[4] == "2020-01-01"
|
||||
assert work_time[5] == "2020-02-01"
|
||||
|
||||
|
||||
class TestEditWorkTimeIFC4X3(test.bootstrap.IFC4X3):
|
||||
|
||||
@@ -44,6 +44,13 @@ class TestFormat():
|
||||
def test_number_formatting(self):
|
||||
assert subject.format("round(123, 5)") == "125"
|
||||
assert subject.format('round(\"123\", 5)') == "125"
|
||||
assert subject.format('number(123)') == "123"
|
||||
assert subject.format('number(1234.56)') == "1,234.56"
|
||||
assert subject.format('number(123, ".")') == "123"
|
||||
assert subject.format('number(\"123\", ".")') == "123"
|
||||
assert subject.format('number(123.12, ".")') == "123.12"
|
||||
assert subject.format('number(123.12, ",")') == "123,12"
|
||||
assert subject.format('number(1234.12, ",", ".")') == "1.234,12"
|
||||
assert subject.format('metric_length(123, 5, 2)') == "125.00"
|
||||
assert subject.format('metric_length(123.123, 0.1, 2)') == "123.10"
|
||||
assert subject.format('metric_length(\"123\", 5, 2)') == "125.00"
|
||||
|
||||
Reference in New Issue
Block a user