This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/rhai_engine/examples/loadscripts/scripts/math_utils.rhai
2025-04-03 12:31:01 +02:00

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))
}