32 lines
642 B
Plaintext
32 lines
642 B
Plaintext
// Math utility functions for Tera templates
|
|
|
|
// Format a number with commas as thousands separators
|
|
fn format_number(num) {
|
|
let str = num.to_string();
|
|
let result = "";
|
|
let len = str.len;
|
|
|
|
for i in 0..len {
|
|
if i > 0 && (len - i) % 3 == 0 {
|
|
result += ",";
|
|
}
|
|
result += str.substr(i, 1);
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
// Calculate percentage
|
|
fn percentage(value, total) {
|
|
if total == 0 {
|
|
return "0%";
|
|
}
|
|
|
|
let pct = (value / total) * 100.0;
|
|
pct.round().to_string() + "%"
|
|
}
|
|
|
|
// Format currency
|
|
fn currency(amount, symbol) {
|
|
symbol + format_number(amount.round(2))
|
|
} |