-
Notifications
You must be signed in to change notification settings - Fork 1
2. Calculations
George Njeri (Swagfin) edited this page Mar 7, 2026
·
1 revision
ObjectSemantics.NET supports lightweight calculation expressions directly in templates.
You can use function names with or without leading underscores.
| Function | Purpose | Example |
|---|---|---|
sum(path) |
Sum numeric values in a path | {{ _sum(Customer.Payments.Amount):N2 }} |
avg(path) |
Average numeric values in a path | {{ _avg(Customer.Payments.PaidAmount):N2 }} |
count(path) |
Count non-null values in a path | {{ _count(Customer.Payments.Amount) }} |
min(path) |
Minimum numeric value in a path | {{ _min(Customer.Payments.Amount):N2 }} |
max(path) |
Maximum numeric value in a path | {{ _max(Customer.Payments.Amount):N2 }} |
calc(expr) |
Arithmetic over properties/literals | {{ _calc(PaidAmount - Customer.CreditLimit):N2 }} |
Example model shape:
Customer.Payments = [
{ Amount = 1000 },
{ Amount = 2000 },
{ Amount = 1500 }
];
Examples:
{{ _sum(Customer.Payments.Amount) }} -> 4500
{{ _avg(Customer.Payments.Amount):N2 }} -> 1,500.00
{{ _count(Customer.Payments.Amount) }} -> 3
{{ _min(Customer.Payments.Amount) }} -> 1000
{{ _max(Customer.Payments.Amount) }} -> 2000
Operators:
+-*/- Parentheses
(...)
Examples:
{{ _calc(PaidAmount - Customer.CreditLimit):N2 }}
{{ _calc((Subtotal + Tax) * 0.5):N2 }}
{{ _calc(Quantity * UnitPrice) }}
{{ #foreach(Items) }}
[{{ Name }}={{ __calc(Quantity * UnitPrice):N2 }}]
{{ #endforeach }}
| Scenario | Result |
|---|---|
| Valid numeric expression/path | Numeric output |
| Null source/property in math path | 0 |
| Unknown path/property in expression | Empty |
Non-numeric data in numeric expression (e.g. string/date for sum) |
Empty |
| Invalid expression syntax | Empty |
Notes:
- These rules apply to
sum,avg,count,min,max, andcalc. - Standard placeholder behavior is different: unknown regular properties remain unresolved (
{{ UnknownProp }}).
Calculation results can use normal format specifiers:
{{ _sum(Customer.Payments.Amount):N2 }}
{{ _calc(PaidAmount - CreditLimit):#,##0 }}
Or no format at all:
{{ _sum(Customer.Payments.Amount) }}
{{ _calc(PaidAmount - CreditLimit) }}